RudderStack JS SDK service worker can be used in serverless runtimes like Cloudflare Workers or Vercel Edge functions.
The provided examples are based on official documentation of the platforms, these contain minimal vercel edge and cloudflare worker implementation.
RudderStack JS SDK service worker can be utilised in Cloudflare workers. You can start with the sample and alter the worker.js file to integrate the sdk:
import { Analytics } from '@rudderstack/analytics-js-service-worker';
const rudderClient = new Analytics(
"<writeKey>",
"<dataplaneUrl>/v1/batch",
{
flushAt: 1
}
);
You can then utilise the RudderStack JS SDK within the fetch methods with promisified flush:
const flush = () => new Promise((resolve) => rudderClient.flush(resolve));
rudderClient.track({
userId: '123456',
event: 'test cloudflare worker',
properties: {
data: {
url: 'test cloudflare worker',
},
},
});
await flush();
See relevant example
RudderStack JS SDK service worker can be utilised in Vercel Edge functions. You can start with the sample and alter the app/api/edge-function-sample/route.ts file to integrate the sdk:
import { Analytics } from '@rudderstack/analytics-js-service-worker';
const rudderClient = new Analytics(
"<writeKey>",
"<dataplaneUrl>/v1/batch",
{
flushAt: 1
}
);
You can then utilise the RudderStack JS SDK within the fetch methods as usual:
rudderClient.track({
userId: '123456',
event: 'test vercel edge worker',
properties: {
data: {
url: 'test vercel edge worker',
},
}
});
See relevant example