# @segment/edge-sdk

> There are two “unsolved” problem with app instrumentation today:

Latest version **0.2.0** (published 2023-10-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @segment/edge-sdk
pnpm add @segment/edge-sdk
yarn add @segment/edge-sdk
bun add @segment/edge-sdk
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2023-10-03 |
| First published | 2022-12-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 73.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | nithin-benny, poojasegment, ebru.odok, bannapple, rodhilton_twilio, guthriesegment, srishti-nema, cdelaomartinez, miguelpdiaz8, sundareswar.jayakumar, gbatra, spencerattick, akodankiry, nanette.ranes, gsolis_segment, nitinpm432, aadityabhatt10, amigandhi, segmentsean, hmohanram_seg, jrupasinghe, myrontin.segment, devthale, smccoy-twilio, akurapati-segment, seg-leonelsanches, jibrang, sethgrid_segment, light-bringer-blr, aubreysine, ed-twilion-npm, harsh-joshi99, irfan.ali.segment, kbhargavaram-sg, needcaffeine, nat-grid, spandananune92, wlumsegment, vivek-26, moyara2, bala.singareddy, gbbastos, sprasannasastry22, akash.gautam07, preetyp, viveksainaneesegment, msaraf, kjoerres, rokatyal, ainatancinco, anton-vylushchak, sowjanyasegment, alayvora, pzawadzki, msaunders-segment, tw-dgarcia, bvakkalagadda, mbreske, nsandhu-segment, parag.panda, blangtwilio, ryanrouleau-segment, twjosiah, mcullenmeyer, nina-y, david.anusontarangkul.segment, mckern_segment, segment-admin, nainy.agrawal, tdibacco, sudojatin, nageshgolem, brandonheyer-segment, teganduong, alfrimpong, dobrin.ganev, ankit.gupta.unthinkable, marinhero, benattwilio, bharath.boregowda, conniechen, sungju.jin, pooyaj, yli119, ea_segment, emilyjia, kx-segment, cvillela_segment, xinghao.huang, harsh.vardhan, joe.ayoub.segment, gkochar123, rollcode, ariel.silvestri, cherylj-segment, leifdreizler, immanoj, aaronklish, michelr, maneesh.dharma29, msolorzano-segment, brianhumphreystwilio, jfehrman.segment, joetessy, pmunin, jalexy12, jbandi-twilio, prayansh-twilio, dominicbarnes, brandon.scott-segment, bgillan, phillip.thomas, ricardo.rossi, forgetfulfellow, fauzy.yy, mayur-pitale, dbaik-twilio-segment, seg-rustybailey, tanya.gupta.segment, pmiller-twilio, nevermore2022, aishikawaki, csayuso, mcoulibali, shupadhyay, swgupta, sumit1606, jahood-twilio, saisagarkappaganthula, dostwal, rmukundan, ngoldman-twilio, apareek-twilio, arubiochavez, shuvrajit9904, s3gm3nt, ama0223, tbrennanj, dangmai-segment, shraddha-twilio, taricjain, snehauppalapati, nitish.dubey, azhaotwilio, ryder.wendt, sa-jsooter, enyi.asonye, afsha-nazim-seg, akurapati, mschaszberger, lnamba, varadarajan-tw, seanhan-segment, replatero, sayan-das-in, justeen, sausingh, jgabe13, haeyoon.paik, meg1000, funlu, ashwitha.bg, whaider_twilio, tcgilbert, kevinburkesegment, felttrip, prabhnoor1997, akashyap91, connormarchand, clintz.seg, karimkeshwani, wally.tg, rhall-twilio, yash-twilio, brookstaylorjr, shayan-golafshani, lerahulram, shpankau, mugelstad, hdamani, brchristy, rrivera-segment, sethnutetwilio, fplinke, manali-bhosale, chtoombs, sethsegment, eric-hyde, elmoselyee, michaelghseg, jayakrishnannair, lateefat, maryam.sharif, wdbetts, ryanligon, sindhusegment, lfdelossantos, aramakrishnan, segment-camden, vbatanov, lluque-twilio, jair.aviles, pmaid, song4you, peterdemartini, emmy.byrne, vincen7tran, dean-huynh, cdignam-segment, abhinavsureka, rikezatsegment, arunlalam-segment, cjradek, neeharikakondipati, simpixelated, chihchun-twilio, acharles14, jyim008, seghungtran, hema-segment, oscb, krousseau, sachinwathore, fhalim-segment, cfree, hjoonpm, celine-segment, parsa-segment, pmcanseco-segment, masira, amillet89, cholt002, av-segment, aghotikar, vikrant-segment, larryatsegment, shivpoojan-segment, ariel_segment, scruwys1, lew-gordon, kyliepedersen, jinapark, segmentio, rajulvadera, lpediredla, n2parko, sahilp, tyson_segment, bgamwell, uditmehta, salolivares, erikdw, chenxiangzhang, mericsson, prayansh-segmentt, jeremylarkin, bsneed, danieljackins, segment-seth, james9446, priscilla.giatti, kellylu, nlsun, drew-thompson, segment-jsingh, andrius-segment, valerieernst, khinkalilover, kelcook, alistairbarrell, gilomer, marcelopv, eric.rogner, kdharaiya, jon.anderson-at-segment.com, stacy.song, niveda.balananthan, rexatsegment, nickaguilar, bradenbecker, reneewang, dan.lasky, sam.tapia, vikramkumar19, mpriyad25, jeremy.parker, smidges |

## Links

- npm: https://www.npmjs.com/package/@segment/edge-sdk
- npm.io page: https://npm.io/package/@segment/edge-sdk

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^9.0.0
- [tldts](https://npm.io/package/tldts.md) ^5.7.100
- [worktop](https://npm.io/package/worktop.md) ^0.8.0-next.14
- [@segment/snippet](https://npm.io/package/@segment/snippet.md) ^4.15.3

## Recent versions

- 0.2.0 (latest) — 2023-10-03
- 0.1.3 — 2023-09-07
- 0.1.2 — 2023-03-29
- 0.1.1 — 2023-03-29
- 0.1.0 — 2023-03-24
- 0.0.8 — 2023-03-22
- 0.0.7 — 2023-03-01
- 0.0.6 — 2022-12-09

## README

# Segment Edge SDK for Cloudflare Workers

There are two “unsolved” problem with app instrumentation today:

**Problem #1:** Many important events that you want to track happen on the “wild-west” of the client, but collecting those events via the client can lead to low data quality, as events are dropped due to user configurations, browser limitations, and network connectivity issues.

**Problem #2:** Applications need access to realtime (<50ms) user state to personalize the application experience based on advanced computations and segmentation logic that must be executed on the cloud.

The Segment Edge SDK – built on Cloudflare Workers – solves for both. With Segment Edge SDK, developers can collect high-quality first-party data. Developers can also use Segment Edge SDK to access realtime user profiles and state, to deliver personalized app experiences without managing a ton of infrastructure.

## Getting Started

Edge SDK requires you to setup a Cloudflare Worker. You can choose one of the following two methods to setup your worker:

### Running as a full reverse proxy on your main domain

This approach allows the worker to intercept all the request to your website, and forward them to one or more backend servers (origins). This can be useful for delivering personalized content, as the worker can use information about the client, such as user identity or traits, to tailor the response from the backend server, such as delivering a customized version of the page to the client. Furthermore, the worker can automatically inject analytics.js to the webpages, in order to make instrumentation more seamless.

To run as a full reverse proxy, you have to deploy your worker using [Routes](https://developers.cloudflare.com/workers/platform/triggers/routes/). Follow these instructions to setup your worker:

✋ As a pre-requisit, you need to sign-up for a Cloudflare account, and add your domain to Cloudflare so that Cloudflare is able to resolve your domain. Use [these instructions](<https://developers.cloudflare.com/learning-paths/get-started/#domain-resolution-(active-website)>) to setup your website with Cloudflare.

1- Follow the [Get Started Guide](https://developers.cloudflare.com/workers/get-started/guide/) to setup a basic Cloudflare worker using Wrangler, and by choosing the default options offered by Wrangler during the setup. After the setup, you should have a directory, with a "Hello World" worker setup.

2- Install the Segment Edge SDK in your worker project

```
yarn add @segment/edge-sdk
or
npm install @segment/edge-sdk
```

3- Update your worker code (`index.ts`) as follows:

```diff
+ import { Segment } from "@segment/edge-sdk";

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
-    return new Response("Hello World!");
+    const segment = new Segment(
+      {
+        writeKey: "YOUR_WRITE_KEY",
+        routePrefix: "magic", // path prefix for serving Segment assets
+        personasSpaceId: "...", // optional
+        personasToken: "...", // optional
+      }
+    );
+
+    const resp = await segment.handleEvent(request);
+    return resp;
  },
};

```

4- Update `wrangler.toml` file so that the worker intercepts requests to the website:

```diff
name = '...'
main = "src/index.ts"

+ route = "www.your_website.com/*"
```

5- Deploy the worker

```
wrangler publish
```

🎉 Now if you visit your website, all the pages are automatically instrumented with analytics.js!

### Running on a dedicated sub-domain

This approach runs the worker on a sub-domain of yours, and the worker will only be responsible for first-party delivery of AJS, and delivering client-side traits. But the worker will not intercept individual pages on your main domain, and therefore features such as Personalization or Automatic AJS Injection won't be available.

To run the worker on a sub-domain, you can deploy your worker using these instructions:
1- Follow steps 1-3 from the previous section
2- Update `wrangler.toml` file so that the worker is setup on a sub-domain in your zone

```diff
name = '...'
main = "src/index.ts"

+ routes = [
+ 	{ pattern = "your_sub_domain.your_website.com", custom_domain = true, zone_name = "your_website.com" }
+ ]
```

3- Modify the worker code to turn-off full proxy features:

```diff
    const segment = new Segment(
      {
        writeKey: "YOUR_WRITE_KEY",
        routePrefix: "magic",
        personasSpaceId: "...", // optional
        personasToken: "...", // optional
      },
      {
+       ajsInjection: false,
+       edgeVariations: false,
+       proxyOrigin: false,
      }
    );

```

4- Since the automatic AJS injection is not available, you have to add Segment snippet to your website manually. Make sure to modify the standard snippet so it points to your first-party domain:

```diff
- t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";
+ t.src="https://your_sub_domain.your_website.com/<your_prefix>/ajs/<random_uuid>"
```

### (Optional) Configuring Edge Storage for Storing Profiles

You can setup Edge Storage to store profiles on Edge and use them for personalization or client-side traits feature. Follow these steps:

1- Setup Cloudflare KV for Profiles Database

- Setup a KV using [these instructions](https://developers.cloudflare.com/workers/wrangler/workers-kv/#create-a-kv-namespace-with-wrangler)
- Update your worker code as follows:

```diff
import { Segment } from "@segment/edge-sdk";

+ export interface Env {
+   MY_KV_NAMESPACE: KVNamespace;
+ }

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    const segment = new Segment(
      {
        writeKey: "YOUR_WRITE_KEY",
        routePrefix: "magic",
        personasSpaceId: "...", // optional
        personasToken: "...", // optional
+       profilesStorage: env.MY_KV_NAMESPACE,
      }
    );

    const resp = await segment.handleEvent(request);
    return resp;
  },
};
```

2- Setup Profiles Sync, using both or one of the sync approaches explained in the next couple of sections.

### Configure Profiles API Access

Edge SDK can query Twilio Segment Profiles API to look for those profiles missing from the KV. Make sure you setup both `personasSpaceId` and `personasToken` settings correctly during the SDK initialization for this feature to work.

### Configure Engage Incoming Webhook

You can also configure a webhook for Twilio Engage to call in order to sync user traits to the Edge database. Follow these steps for configure your webhook:

1- Initialize SDK with a webhook username and password

```diff
    const segment = new Segment(
      {
        writeKey: "YOUR_WRITE_KEY",
        routePrefix: "magic",
        personasSpaceId: "...", // optional
        personasToken: "...", // optional
        profilesStorage: env.MY_KV_NAMESPACE,
+       engageWebhookUsername: "" // choose a username
+       engageWebhookPassword: "" // choose a password
      }
    );
```

2- Goto your Segment workspace in `app.segment.com` and visit `Engage>Engage Settings` page
3- Click on "+ Add Destination"
4- Choose "Webhook" and connect it to your Personas source
5- In the destination settings configure your Webhook URL as the URL of your website that runs the Edge SDK with following format `https://your_first_party_url/<routePrefix>/personas` and then add a `Authorization` header with the value of `Basic <base64 encoded value of username:password>`.
6- Go to each of your Audiences that you like to sync with Edge, connect your webhook destination to the Audience, and make sure the destination is configured with "Send Identify".

🎉 You are all set. Twilio Engage will start syncing your audiences with your worker!

## API

**Constructor**

```typescript
const segment = new Segment(settings, features);
```

Checkout the JSDoc on each settings, and features parameters.

**`.handleEvent(request: Request)`**

Returns: `Response`

Accepts a Cloudflare incoming Request, and returns a response that could be AJS assets, response from Segment Tracking API, or response from the Origin.

**`.registerVariation(route: string, evaluationFunction: VariationEvaluationFunction)`**

Retruns: `undefined`

Register a variation on the `route`. If a visitor navigates to `route`, we run the evaluationFunction, and fetch the path returned from the function instead of the `route`. If the evaluationFunction returns `undefined`, we then fetch the `route` from the origin.

**`.clientSideTraits(traitsFunc: TraitsFunction)`**

Returns: `undefined`

Registers a function that transform the visitors full `audiences` list to a redacted version, and then deliver the redacted audiences to the client.

## SDK Features

### Collecting Edge Context ( `features.edgeContext` )

The SDK will automatically capture the information available on the edge and include them in the `context.edge` object of the Segment event.

👉 the `context.edge` will be available on `track`, `identify` and `page` calls
👉 The following information will be collected:

- region
- regionCode
- city
- country
- continent
- postalCode
- latitude
- longitude
- timezone

### Automatic Injection of AJS to pages ( `features.ajsInjection` )

The SDK will automatically adds AJS snippet to the `<HEAD>` of each page.

👉 This feature should only be used if the SDK is being used as a full proxy of the origin.
👉 You can use `snippetInitialPageView` when initializing the Edge SDK to control the initial page call for the injected snippet. For example:

```javascript
const segment = new Segment({
  writeKey: "...",
  routePrefix: "myPrefix",
  snippetInitialPageView: false, // skips the initial page call in the snippet
});
```

### Server-side Cookies ( `features.serverSideCookies` )

The Edge SDK will set ajs_anonymous_id and ajs_user_id cookies as HTTPOnly. This feature has several benefits:

👉 It prevents Safari browsers from clearing those ids, as currently Safari limits the TTL of JS accessible storage to 7 days.
👉 It prevents client-side code from accessing those identifiers.
There are a few important considerations regarding the server-side cookies:

There are a few important considerations regarding the server-side cookies:

👉 Users visiting a website for the first time will receive an `anonymousId` that is generated on the Edge and stored as an HTTPOnly cookie.
👉 If a user is already identified on a given browser (has either an `anonymousId` or `userId`), the Edge SDK will respect those identities but convert them from client-side cookies to server-side cookies.

👉 If a user's identity is updated through a call to the `identify` / `track` / `page` / `group` methods, the identity is initially stored as a client-side cookie. When the browser receives the response from the Edge, the cookie is promoted to a server-side cookie.

👉 If a user's identity is updated through `analytics.setAnonymousId()` or ` analytics.user().id(``'``…') `, the identity is initially set as a client-side cookie. On the next request (either a tracking call or when AJS loads again), the cookie is promoted to an HTTPOnly cookie.

👉 If the identity of the user is cleared using `analytics.reset()`, the identity is initially cleared from the browser, and subsequently, browser notifies to worker to clear the server-side cookies.

### Redact writekeys ( `features.redactWritekey` )

The Edge SDK has the ability to remove the writeKey from AJS, Settings, and calls to the tracking API, and then add the writeKey to events from the Edge. This allows you to keep your writeKey private and not expose it on the browser. Note that writeKeys are typically public, but this feature gives you the option to keep it hidden on the browser.

### Edge Variation ( `features.edgeVariations` )

This is a personalization feature and requires the Edge SDK to be setup as a full proxy. When this feature is turned on, you can use the `registerVariation` to chose what content to serve on a given route:

```typescript
segment.registerVariation("/", (audiences) => {
  if (!audiences) {
    return;
  }
  return audiences.vancouver_crew ? "/van" : "/sf";
});
```

The `registerVariation` accepts two paramters:

- `route` : this is the route that serves the personalized content
- `evaluationFunction` : this is the method that determines where the personalized content is coming from, and should return a path in the origin that contains the personalized content. The evaluation function receives the list of `audiences` for the current visitor, and can choose the origin path using the information in audiences. If the function returns `undefined`, the SDK just serves the default content from origin.

### Client Side Traits ( `features.clientSideTraits` )

The SDK can expose a reduced set of user traits to the client. By using the `clientSideTraits` method, the Edge SDK transforms the `audiences` object to a reduced form that can be exposed to the client, and then sets the reduced audiences as a client-side trait in Analytics.js.

```typescript
segment.clientSideTraits((audiences) => {
  return {
    group_a: audiences.vancouver_crew,
  };
});
```

### Engage Incoming Webhook ( `features.engageIncomingWebhook` )

The `features.engageIncomingWebhook` option enables a webhook that can be used by Twilio Engage to send audience data to the Cloudflare worker. This feature is useful for integrating the Edge SDK with Twilio Engage, allowing you to use audience information from Twilio Engage in your personalization and other features powered by the Edge SDK.

### Using Profiles API for identity resolution ( `features.useProfilesAPI` )

The SDK will use the Twilio Segment Profiles API, if the user profile is not available on the Edge. If turning on this feature, you should provide the Profiles API crendentials during the SDK initialization.

### Proxy Origin (`proxyOrigin`)

This feature will allow the SDK to proxy all the calls to the origin. You should only use this feature if running the SDK as a full proxy.

---
_Source: https://npm.io/package/@segment/edge-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
