Skip to content

Commit

Permalink
added anonymous id in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
prabrisha-rudder committed Nov 22, 2019
1 parent d208b33 commit f92b227
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
27 changes: 17 additions & 10 deletions rudder-client-javascript/analytics/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ class Analytics {
this.userId =
this.storage.getUserId() != undefined
? this.storage.getUserId()
: generateUUID();
: "";

this.userTraits =
this.storage.getUserTraits() != undefined
? this.storage.getUserTraits()
: {};

this.anonymousId = this.storage.getAnonymousId() ? this.storage.getAnonymousId() : generateUUID();

this.storage.setUserId(this.userId);
this.storage.setAnonymousId(this.anonymousId);
this.eventRepository = EventRepository;
}

Expand Down Expand Up @@ -331,7 +334,7 @@ class Analytics {
* @memberof Analytics
*/
processIdentify(userId, traits, options, callback) {
if(userId !== this.userId){
if( userId && this.userId && userId !== this.userId){
this.reset();
}
this.userId = userId;
Expand Down Expand Up @@ -424,20 +427,23 @@ class Analytics {
*/
processAndSendDataToDestinations(type, rudderElement, options, callback) {
try {
if (!this.userId) {
/* if (!this.userId) {
this.userId = generateUUID();
this.storage.setUserId(this.userId);
} */
if(!this.anonymousId){
console.log("anonymousId not present: " + this.storage.getAnonymousId());
this.anonymousId = generateUUID();
this.storage.setAnonymousId(this.anonymousId);
}

rudderElement["message"]["context"]["traits"] = Object.assign(
{},
this.userTraits
);
rudderElement["message"]["anonymousId"] = rudderElement["message"][
"userId"
] = rudderElement["message"]["context"]["traits"][
"anonymousId"
] = this.userId;
console.log("anonymousId: ", this.anonymousId)
rudderElement["message"]["anonymousId"] = this.anonymousId;
rudderElement["message"]["userId"] = this.userId;

if (options) {
this.processOptionsParam(rudderElement, options);
Expand Down Expand Up @@ -489,9 +495,9 @@ class Analytics {
if (toplevelElements.includes(key)) {
rudderElement.message[key] = options[key];
//special handle for ananymousId as transformation expects anonymousId in traits.
if (key === "anonymousId") {
/* if (key === "anonymousId") {
rudderElement.message.context.traits["anonymousId"] = options[key];
}
} */
} else {
if (key !== "context")
rudderElement.message.context[key] = options[key];
Expand Down Expand Up @@ -522,6 +528,7 @@ class Analytics {
reset() {
this.userId = "";
this.userTraits = {};
this.anonymousId = "";
this.storage.clear();
}

Expand Down
7 changes: 3 additions & 4 deletions rudder-client-javascript/analytics/utils/RudderMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ class RudderMessage {
this.action = null;
this.messageId = generateUUID().toString();
this.originalTimestamp = new Date().toISOString();
this.anonymousId = generateUUID().toString();
this.anonymousId = null;
this.userId = null;
this.event = null;
this.properties = {};

this.integrations = {};
//By default, all integrations will be set as enabled from client
//Decision to route to specific destinations will be taken at server end
this.integrations = { All: false, S3: true };
//this.integrations["All"] = true;
this.integrations["All"] = true;
}

//Get property
Expand Down
15 changes: 14 additions & 1 deletion rudder-client-javascript/analytics/utils/storage/storage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logger from "../logUtil"
let defaults = {
user_storage_key: "rl_user_id",
user_storage_trait: "rl_trait"
user_storage_trait: "rl_trait",
user_storage_anonymousId: "rl_anonymous_id"
};
class Storage {
constructor() {
Expand Down Expand Up @@ -33,6 +34,14 @@ class Storage {
this.storage.setItem(defaults.user_storage_trait, JSON.stringify(value));
return;
}
setAnonymousId(value) {
if (typeof value != "string") {
logger.error("anonymousId should be string");
return;
}
this.storage.setItem(defaults.user_storage_anonymousId, value);
return;
}
getItem(key) {
let stringValue = this.storage.getItem(key);
return JSON.parse(stringValue);
Expand All @@ -43,12 +52,16 @@ class Storage {
getUserTraits() {
return JSON.parse(this.storage.getItem(defaults.user_storage_trait));
}
getAnonymousId() {
return this.storage.getItem(defaults.user_storage_anonymousId);
}
removeItem(key) {
this.storage.removeItem(key);
}
clear() {
this.storage.removeItem(defaults.user_storage_key);
this.storage.removeItem(defaults.user_storage_trait);
this.storage.removeItem(defaults.user_storage_anonymousId);
}
}

Expand Down

0 comments on commit f92b227

Please sign in to comment.