jitsucom / jitsu

Jitsu is an open-source Segment alternative. Fully-scriptable data ingestion engine for modern data teams. Set-up a real-time data pipeline in minutes, not days
https://jitsu.com
MIT License
4.13k stars 295 forks source link

setAnonymousId not persisting when calling track with @jitsu/js #1060

Closed otaku-tekkurai closed 1 year ago

otaku-tekkurai commented 1 year ago

Description:

I am attempting to set the anonymous ID using setAnonymousId, but when I call track("EVENT_1", {}), the anonymous ID is null or something different. However, the anonymous ID of EVENT_2 is as expected.

Code Snippet:

   const { jitsuAnalytics } = require("@jitsu/js");
   const { v4: uuidv4 } = require("uuid");

   const config = {
       host: "http://jitsu-develop.d.in/",
       writeKey: "your_write_key_here",
       debug: true
   };

  const setupJitsu = async () => {
    try {
      const client = jitsuAnalytics(config);

      if (client) {
          const anonymousId = uuidv4();
          console.log("Anonymous ID:", anonymousId);

          await client.setAnonymousId(anonymousId);
          console.log("anonymousId setup");

          await client.track("EVENT_1", {});
          console.log("Event 1 tracked"); // not as expected

          await client.track("EVENT_2", {});
          console.log("Event 2 tracked"); // as expected
      }
    } catch (error) {
        console.error("Error setting up Jitsu client:", error);
    }
  }

  setupJitsu()

Logs:

[JITSU EMPTY RUNTIME] Get storage item __user_id=undefined
[JITSU EMPTY RUNTIME] Get storage item __anon_id=undefined
[JITSU EMPTY RUNTIME] Get storage item __user_traits=undefined
Anonymous ID: 69592ef2-1db7-4713-9f17-8d166aad014a
anonymousId setup
[JITSU EMPTY RUNTIME] Get storage item __user_id=undefined
[JITSU EMPTY RUNTIME] Get storage item __anon_id=undefined
[JITSU EMPTY RUNTIME] Get storage item __user_traits=undefined
[JITSU EMPTY RUNTIME] Set storage item __anon_id="cc0b9784-469c-42c1-8048-7e29035b3dc0"
[JITSU EMPTY RUNTIME] Set storage item __anon_id="69592ef2-1db7-4713-9f17-8d166aad014a"
[JITSU] Initializing Jitsu plugin with config:  {"writeKey":"***:***","host":"http://jitsu-develop.d.in","debug":true,"fetch":null,"echoEvents":false}
[JITSU EMPTY RUNTIME] Get storage item __user_traits=undefined
[JITSU] http://jitsu-develop.d.in/api/s/track replied 200. Original payload:  {
  "type": "track",
  "event": "EVENT_1",
  "properties": {},
  "userId": null,
  "anonymousId": "cc0b9784-469c-42c1-8048-7e29035b3dc0",
  "timestamp": "2023-11-15T23:45:30.295Z",
  "sentAt": "2023-11-15T23:45:30.295Z",
  "messageId": "1glauk7uyuovpmlmfnebg",
  "writeKey": "***:***",
  "context": {
    "library": {
      "name": "@jitsu/js",
      "version": "0.0.0"
    },
    "traits": {},
    "page": {},
    "clientIds": {
      "ga4": {}
    },
    "campaign": {}
  }
}
Event 1 tracked
[JITSU EMPTY RUNTIME] Get storage item __user_id=undefined
[JITSU EMPTY RUNTIME] Get storage item __anon_id="69592ef2-1db7-4713-9f17-8d166aad014a"
[JITSU EMPTY RUNTIME] Get storage item __user_traits=undefined
[JITSU EMPTY RUNTIME] Get storage item __user_traits=undefined
[JITSU] http://jitsu-develop.d.in/api/s/track replied 200. Original payload:  {
  "type": "track",
  "event": "EVENT_2",
  "properties": {},
  "userId": null,
  "anonymousId": "69592ef2-1db7-4713-9f17-8d166aad014a",
  "timestamp": "2023-11-15T23:45:31.469Z",
  "sentAt": "2023-11-15T23:45:31.469Z",
  "messageId": "1ot6x6srs6b2gj3r7gngf8",
  "writeKey": "***:***",
  "context": {
    "library": {
      "name": "@jitsu/js",
      "version": "0.0.0"
    },
    "traits": {},
    "page": {},
    "clientIds": {
      "ga4": {}
    },
    "campaign": {}
  }
}
Event 2 tracked

Environment:

vklimontovich commented 1 year ago

This is fixed, please upgrade to 1.7.2