Skip to content

Commit

Permalink
added logic for showing warning when reserved keywords ae sent in pro…
Browse files Browse the repository at this point in the history
…perties/traits
  • Loading branch information
ruchiramoitra authored and sayan-mitra committed Jan 7, 2021
1 parent 5355040 commit 40a52c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
27 changes: 25 additions & 2 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
CONFIG_URL,
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL,
reservedPropKeywords,
} from "./utils/constants";
import { integrations } from "./integrations";
import RudderElementBuilder from "./utils/RudderElementBuilder";
Expand Down Expand Up @@ -497,6 +498,11 @@ class Analytics {
if (!properties) {
properties = {};
}
Object.keys(properties).forEach((property) => {
if (reservedPropKeywords.indexOf(property) >= 0) {
console.error(`Warning! : Reserved keyword used --> ${property} with page call for name --> ${name}`);
}
});
if (name) {
rudderElement.message.name = name;
properties.name = name;
Expand Down Expand Up @@ -529,7 +535,11 @@ class Analytics {
} else {
rudderElement.setProperty({});
}

Object.keys(properties).forEach((property) => {
if (reservedPropKeywords.indexOf(property) >= 0) {
console.error(`Warning! : Reserved keyword used --> ${property} with track call for event --> ${event}`);
}
});
this.trackEvent(rudderElement, options, callback);
}

Expand All @@ -553,6 +563,11 @@ class Analytics {
.setType("identify")
.build();
if (traits) {
Object.keys(traits).forEach((trait) => {
if (reservedPropKeywords.indexOf(trait) >= 0) {
console.error(`Warning! : Reserved keyword used --> ${trait} with identify call for user --> ${userId}`);
}
});
for (const key in traits) {
this.userTraits[key] = traits[key];
}
Expand All @@ -574,15 +589,22 @@ class Analytics {
this.userId = rudderElement.message.userId;
this.storage.setUserId(this.userId);
}
let traits;

if (
rudderElement &&
rudderElement.message &&
rudderElement.message.context &&
rudderElement.message.context.traits
) {
traits = rudderElement.message.context.traits;
Object.keys(traits).forEach((trait) => {
if (reservedPropKeywords.indexOf(trait) >= 0) {
console.error(`Warning! : Reserved keyword used --> ${trait} with identify call for user --> ${this.userId}`);
}
});
this.userTraits = {
...rudderElement.message.context.traits,
...traits,
};
this.storage.setUserTraits(this.userTraits);
}
Expand Down Expand Up @@ -1177,3 +1199,4 @@ export {
getAnonymousId,
setAnonymousId,
};

12 changes: 12 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// Reserved Keywords for properties/triats
const reservedPropKeywords = [
"anonymous_id",
"id",
"sent_at",
"received_at",
"timestamp",
"original_timestamp",
"event_text",
"event",
];
// Message Type enumeration
const MessageType = {
TRACK: "track",
Expand Down Expand Up @@ -79,6 +90,7 @@ const MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
const INTEGRATION_LOAD_CHECK_INTERVAL = 1000;

export {
reservedPropKeywords,
MessageType,
ECommerceParamNames,
ECommerceEvents,
Expand Down

0 comments on commit 40a52c6

Please sign in to comment.