Skip to content

Commit

Permalink
feat (@jitsu/js): Add defaultPayloadContext to include default cont…
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeremyanin authored Dec 17, 2024
1 parent 78ef2d4 commit 1016c3a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
27 changes: 27 additions & 0 deletions libs/jitsu-js/__tests__/node/nodejs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,33 @@ describe("Test Jitsu NodeJS client", () => {
expect((p.body.anonymousId ?? "").length).toBeGreaterThan(0);
});

test("test defaultPayloadContext", async () => {
const config = {
host: server.baseUrl,
writeKey: "key:secret",
debug: true,
defaultPayloadContext: {
awesomeIdentifier: "awesome-identifier",
awesome: {
nestedKey: "awesome-key",
},
},
};
const client = jitsuAnalytics(config);
expect(requestLog.length).toBe(0);
await client.identify("myUserId", { email: "[email protected]" });
await client.group("myGroupId", { name: "myGroupId" });
await client.track("myEvent", { prop1: "value1" });
await new Promise(resolve => setTimeout(resolve, 1000));
expect(requestLog.length).toBe(3);
expect(requestLog[0].body.context.awesomeIdentifier).toBe("awesome-identifier");
expect(requestLog[0].body.context.awesome.nestedKey).toBe("awesome-key");
expect(requestLog[1].body.context.awesomeIdentifier).toBe("awesome-identifier");
expect(requestLog[1].body.context.awesome.nestedKey).toBe("awesome-key");
expect(requestLog[2].body.context.awesomeIdentifier).toBe("awesome-identifier");
expect(requestLog[2].body.context.awesome.nestedKey).toBe("awesome-key");
});

test("node js", async () => {
const jitsu: AnalyticsInterface = jitsuAnalytics({
writeKey: "key:secret",
Expand Down
6 changes: 5 additions & 1 deletion libs/jitsu-js/src/analytics-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const defaultConfig: Required<JitsuOptions> = {
s2s: undefined,
idEndpoint: undefined,
errorPolicy: "log",
defaultPayloadContext: {},
privacy: {
dontSend: false,
disableUserIds: false,
Expand Down Expand Up @@ -445,7 +446,10 @@ function adjustPayload(
properties.path = fixPath(urlPath(targetUrl));
}

const customContext = payload.properties?.context || payload.options?.context || {};
const customContext = deepMerge(
config.defaultPayloadContext,
payload.properties?.context || payload.options?.context || {}
);
delete payload.properties?.context;
const referrer = runtime.referrer();
const context: AnalyticsClientEvent["context"] = {
Expand Down
9 changes: 9 additions & 0 deletions types/protocols/analytics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ export type JitsuOptions = {
* To enable debug logging
*/
debug?: boolean;
/**
* Default payload context to be included in all requests.
* These attributes are merged with the request-specific payload context,
* allowing common or global data (e.g., browser timezone, some extra user identifier)
* to be automatically included to every request.
*
* The context can be a nested structure.
*/
defaultPayloadContext?: Record<string, JSONValue>;
/**
* Explicitly specify cookie domain. If not set, cookie domain will be set to top level
* of the current domain. Example: if JS lives on "app.example.com", cookie domain will be
Expand Down

0 comments on commit 1016c3a

Please sign in to comment.