Skip to content

Commit

Permalink
fix: setAnonumousId() fixed in @jitsu/js. Fixes GH #1060
Browse files Browse the repository at this point in the history
  • Loading branch information
vklimontovich committed Nov 16, 2023
1 parent 7783955 commit fae7fde
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 44 deletions.
38 changes: 38 additions & 0 deletions libs/jitsu-js/__tests__/node/nodejs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("Test Jitsu NodeJS client", () => {
let requestLog: { type: string; body: AnalyticsClientEvent }[] = [];

const startServer = async () => {
requestLog = [];
server = await createServer({
port: 3088,
https: false,
Expand All @@ -30,8 +31,45 @@ describe("Test Jitsu NodeJS client", () => {
const shutdownServer = async () => {
console.log("Shutting down server " + server.baseUrl);
await server.close();
console.log("Server is down " + server.baseUrl);
};

test("setAnonymousId test2", async () => {
await startServer();

const config = {
host: server.baseUrl,
writeKey: "key:secret",
debug: true,
};

try {
console.log("[JITSU TEST] Initializing Jitsu");
const client = jitsuAnalytics(config);
console.log("[JITSU TEST] Jitsu instance", client);

const anonymousId = "anonymous_id_test";
console.log("[JITSU TEST] Setting anonymous id to " + anonymousId);
await client.setAnonymousId(anonymousId);
console.log("user state", client.getState("user"));

expect(requestLog.length).toBe(0);
console.log("[JITSU TEST] Sending event EVENT_1");
await client.track("EVENT_1");
await client.track("EVENT_2");
await client.track("groupId");

await new Promise(resolve => setTimeout(resolve, 1000));

expect(requestLog.length).toBe(3);
expect(requestLog[1].body.anonymousId).toBe("anonymous_id_test");
expect(requestLog[0].body.anonymousId).toBe("anonymous_id_test");
expect(requestLog[2].body.anonymousId).toBe("anonymous_id_test");
} finally {
shutdownServer();
}
});

test("node js", async () => {
await startServer();
try {
Expand Down
2 changes: 1 addition & 1 deletion libs/jitsu-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
"typescript": "^4.9.5"
},
"dependencies": {
"analytics": "^0.8.1"
"analytics": "0.8.9"
}
}
9 changes: 8 additions & 1 deletion libs/jitsu-js/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ const commonjs = require("@rollup/plugin-commonjs");
const rollupJson = require("@rollup/plugin-json");
const terser = require("@rollup/plugin-terser");


module.exports = [
{
plugins: [multi(), resolve({ preferBuiltins: false }), commonjs(), rollupJson(), terser()],
plugins: [
multi(),
resolve({ preferBuiltins: false }),
commonjs(),
rollupJson(),
(process.JITSU_JS_DEBUG_BUILD = "1" ? undefined : terser()),
],
input: "./compiled/src/browser.js",
output: {
file: `dist/web/p.js.txt`,
Expand Down
25 changes: 20 additions & 5 deletions libs/jitsu-js/src/analytics-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function send(
store: PersistentStorage
): Promise<void> {
if (jitsuConfig.echoEvents) {
console.log(`[JITSU] sending '${method}' event:`, payload);
console.log(`[JITSU DEBUG] sending '${method}' event:`, payload);
return;
}

Expand Down Expand Up @@ -545,18 +545,31 @@ function send(

const jitsuAnalyticsPlugin = (pluginConfig: JitsuOptions = {}): AnalyticsPlugin => {
const storageCache: any = {};

// AnalyticsInstance's storage is async somewhere inside. So if we make 'page' call right after 'identify' call
// 'page' call will load traits from storage before 'identify' call had a change to save them.
// to avoid that we use in-memory cache for storage
const cachingStorageWrapper = (persistentStorage: PersistentStorage): PersistentStorage => ({
setItem(key: string, val: any) {
if (pluginConfig.debug) {
console.log(`[JITSU DEBUG] Caching storage setItem: ${key}=${val}`);
}
storageCache[key] = val;
persistentStorage.setItem(key, val);
},
getItem(key: string) {
return storageCache[key] || persistentStorage.getItem(key);
const value = storageCache[key] || persistentStorage.getItem(key);
if (pluginConfig.debug) {
console.log(
`[JITSU DEBUG] Caching storage getItem: ${key}=${value}. Evicted from cache: ${!storageCache[key]}`
);
}
return value;
},
removeItem(key: string) {
if (pluginConfig.debug) {
console.log(`[JITSU DEBUG] Caching storage removeItem: ${key}`);
}
delete storageCache[key];
persistentStorage.removeItem(key);
},
Expand All @@ -568,10 +581,11 @@ const jitsuAnalyticsPlugin = (pluginConfig: JitsuOptions = {}): AnalyticsPlugin
return {
name: "jitsu",
config: instanceConfig,

initialize: args => {
const { config } = args;
if (config.debug) {
console.debug("[JITSU] Initializing Jitsu plugin with config: ", JSON.stringify(config));
console.debug("[JITSU DEBUG] Initializing Jitsu plugin with config: ", JSON.stringify(config, null, 2));
}
if (!config.host && !config.echoEvents) {
throw new Error("Please specify host variable in jitsu plugin initialization, or set echoEvents to true");
Expand Down Expand Up @@ -600,15 +614,16 @@ const jitsuAnalyticsPlugin = (pluginConfig: JitsuOptions = {}): AnalyticsPlugin
//analytics doesn't support group as a base method, so we need to add it manually
group(groupId?: ID, traits?: JSONObject | null, options?: Options, callback?: Callback) {
const analyticsInstance = this.instance;
const cacheWrap = cachingStorageWrapper(analyticsInstance.storage);
const user = analyticsInstance.user();
const userId = options?.userId || user?.userId;
const anonymousId = options?.anonymousId || user?.anonymousId;
const anonymousId = options?.anonymousId || user?.anonymousId || cacheWrap.getItem("__anon_id");
return send(
"group",
{ type: "group", groupId, traits, ...(anonymousId ? { anonymousId } : {}), ...(userId ? { userId } : {}) },
instanceConfig,
analyticsInstance,
cachingStorageWrapper(analyticsInstance.storage)
cacheWrap
);
},
},
Expand Down
19 changes: 17 additions & 2 deletions libs/jitsu-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default function parse(input) {
return value;
}

export const emptyAnalytics = {
export const emptyAnalytics: AnalyticsInterface = {
setAnonymousId: () => {},
track: () => Promise.resolve(),
page: () => Promise.resolve(),
user: () => ({}),
Expand All @@ -37,10 +38,11 @@ function createUnderlyingAnalyticsInstance(
rt: RuntimeFacade,
plugins: any[] = []
): AnalyticsInterface {
const storage = rt.store();
const analytics = Analytics({
app: "test",
debug: !!opts.debug,
storage: rt.store(),
storage,
plugins: [jitsuAnalyticsPlugin(opts), ...plugins],
} as any);
const originalPage = analytics.page;
Expand All @@ -56,6 +58,19 @@ function createUnderlyingAnalyticsInstance(
};
return {
...analytics,
setAnonymousId: (id: string) => {
if (opts.debug) {
console.log("[JITSU DEBUG] Setting anonymous id to " + id);
//Workaround for analytics.js bug. Underlying setAnonymousId doesn't work set the id immediately,
//so we got to it manually here. See https://github.com/jitsucom/jitsu/issues/1060
storage.setItem("__anon_id", id);
const userState = analytics.user();
if (userState) {
userState.anonymousId = id;
}
(analytics as any).setAnonymousId(id);
}
},
group(groupId?: ID, traits?: JSONObject | null, options?: Options, callback?: Callback): Promise<DispatchedEvent> {
for (const plugin of Object.values(analytics.plugins)) {
if (plugin["group"]) {
Expand Down
70 changes: 35 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions types/protocols/analytics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export interface AnalyticsInterface {

user(): any;

setAnonymousId(id: string): void;

// alias(
// to: string | number,
// from?: string | number | Options,
Expand Down

2 comments on commit fae7fde

@vercel
Copy link

@vercel vercel bot commented on fae7fde Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

new-jitsu-ee-api – ./webapps/ee-api

ee.jitsu.dev
onetag-ee-api.vercel.app
new-jitsu-ee-api.staging.jitsu.com
new-jitsu-ee-api-git-newjitsu.staging.jitsu.com

@vercel
Copy link

@vercel vercel bot commented on fae7fde Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

new-jitsu – ./webapps/console

ag.ru
logu.au
ozon.ru
sse.ere
erxes.io
baidu.dom
sambla.se
bobsec.com
sambla.com
agro4u.life
bluetick.ai
protontv.eu
t.quenti.io
alicesec.com
dev.aclis.io
docs.dh19.de
docs.dh19.eu
joseviso.com
mydomain.dom
t.democo.dev
t.shoppub.io
t2.jitsu.com
timeplus.com
zoopsign.com
*.d.jitsu.com
beta.mitzu.io
d.versatus.io
data.light.so
data.loudy.co
data.schej.it
imusician.app
imusician.pro
jitsu.logu.au
jitsu.www1.ru
t.thequack.ai
thinkr.com.br
use.jitsu.com
usepolygon.io
www.sambla.se
ajewellers.com
data.uselog.io
gpt.whatfa.com
sidetrekai.com
t.papermark.io
t.saasmonk.app
use2.jitsu.com
www.kellen.top
*.dataspecc.com
app.bluetick.ai
data.askloan.tw
enterticket.com
events.mitzu.io
jitsu.efeer.com
jitsu.ivve.tech
krestomatio.com
sevenbillion.co
xrt.webxr.tools
data.mysitee.com
dev-t.democo.dev
analytics.mtrsvc.com
data.embeddables.com
mercury.stagehub.com
store.sidetrekai.com
teslahenry.github.io
data.hogarlylabs.tech
event.clickncruise.hu
event.clickncruise.ro
test-domain.jitsu.com
teste.fazcomex.com.br
analytics.dev.knekt.io
loraboutiquedental.com
notion.twelftree.co.uk
dev-portal.zoopsign.com
event.tradejobsnz.co.nz
savvy-replay.jitsu.tech
data.analytics-smart.com
event.clickncruise.co.uk
jt.fairhopeweb.github.io
savvy-replay2.jitsu.tech
savvy-replay3.jitsu.tech
savvy-replay4.jitsu.tech
track.alquimiaweb.com.br
track.pressance-group.jp
track.uniquecafes.com.br
colectha.agenciavoolu.com
kolectha.agenciavoolu.com
lp.loraboutiquedental.com
stage-portal.zoopsign.com
new-jitsu.staging.jitsu.com
lodercom-colectha.voolu.shop
warehouse1.trendstyle.com.au
d0.livingdesignsfurniture.com
jitsu.precisaosistemas.com.br
analytics.inspiresolutions.app
canvas.livingdesignsfurniture.com
analytics.dev.inspiresolutions.app
clm2jikrm00002v6r5l6niws3.d.jitsu.com
new-jitsu-git-newjitsu.staging.jitsu.com
3000-rajaraodv-customerdemo-nmpsqwflswt.ws-us102.gitpod.io
new.jitsu.dev

Please sign in to comment.