Skip to content

Commit

Permalink
code review comments incorporation and separate api for call with rud…
Browse files Browse the repository at this point in the history
…derElementBuilder
  • Loading branch information
prabrisha-rudder committed Oct 2, 2019
1 parent 2526118 commit 89155bb
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 38 deletions.
175 changes: 142 additions & 33 deletions rudder-client-javascript/analytics/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ if (process.prod) {

function flush(rudderElement) {
if (!this.eventRepository) {
//console.log("initialize event repo")
this.eventRepository = EventRepository;
}
this.eventRepository.flush(rudderElement);
Expand Down Expand Up @@ -48,8 +47,6 @@ class Analytics {
}

processResponse(status, response) {
//console.log("from callback " + this.prop1);
//console.log(response);
response = JSON.parse(response);
response.source.destinations.forEach(function(destination, index) {
console.log(
Expand All @@ -67,7 +64,6 @@ class Analytics {
this.configArray.push(destination.config);
}
}, this);
//init.call(this, this.clientIntegrations, this.configArray);
this.init(this.clientIntegrations, this.configArray);
}

Expand All @@ -80,12 +76,9 @@ class Analytics {
return;
}
intgArray.forEach(intg => {
//console.log("--name--", intg);
let intgClass = integrations[intg];
//console.log("--class-- ", intgClass);
if (intg === "HS") {
let hubId = configArray[i].hubId;
//console.log("==hubId== " + hubId);
hubId = "6405167";
let intgInstance = new intgClass(hubId);
intgInstance.init();
Expand All @@ -99,10 +92,6 @@ class Analytics {
this.toBeProcessedByIntegrationArray.forEach(event => {
let methodName = event[0];
event.shift();
/* console.log(
"replay on integrations " + "method " + methodName + " args " + event
); */
//uncomment to send data to destination
this.clientIntegrationObjects[i][methodName](...event);
});
}
Expand All @@ -122,18 +111,38 @@ class Analytics {
(options = properties), (properties = name), (name = null);
if (typeof category === "string" && typeof name !== "string")
(name = category), (category = null);
this.processPage(category, name, properties, options, callback);

}

track(event, properties, options, callback) {
if (typeof options == "function") (callback = options), (options = null);
if (typeof properties == "function")
(callback = properties), (options = null), (properties = null);

this.processTrack(event, properties, options, callback);
}

identify(userId, traits, options, callback) {
if (typeof options == "function") (callback = options), (options = null);
if (typeof traits == "function")
(callback = traits), (options = null), (traits = null);
if (typeof userId == "object")
(options = traits), (traits = userId), (userId = this.userId);

this.processIdentify(userId, traits, options, callback);
}

processPage(category, name, properties, options, callback){
if (!this.userId) {
this.userId = generateUUID();
this.storage.setUserId(this.userId);
}

let rudderElement = new RudderElementBuilder().setType("page").build();
//console.log("arg length ",arguments.length)
let methodArguments = arguments; //arguments[0]
if (name) {
console.log("name ", name);
rudderElement["rl_message"]["rl_name"] = name; //JSON.parse(arguments[1]);
rudderElement["rl_message"]["rl_name"] = name;
}
if (category) {
if (!properties) {
Expand All @@ -143,7 +152,7 @@ class Analytics {
}
if (properties) {
console.log(JSON.parse(JSON.stringify(properties)));
rudderElement["rl_message"]["rl_properties"] = properties; //JSON.parse(arguments[1]);
rudderElement["rl_message"]["rl_properties"] = properties;
}

rudderElement["rl_message"]["rl_context"]["rl_traits"] = this.userTraits;
Expand Down Expand Up @@ -175,11 +184,7 @@ class Analytics {
}
}

track(event, properties, options, callback) {
if (typeof options == "function") (callback = options), (options = null);
if (typeof properties == "function")
(callback = properties), (options = null), (properties = null);

processTrack(event, properties, options, callback){
if (!this.userId) {
this.userId = generateUUID();
this.storage.setUserId(this.userId);
Expand Down Expand Up @@ -224,13 +229,7 @@ class Analytics {
}
}

identify(userId, traits, options, callback) {
if (typeof options == "function") (callback = options), (options = null);
if (typeof traits == "function")
(callback = traits), (options = null), (traits = null);
if (typeof userId == "object")
(options = traits), (traits = userId), (userId = this.userId);

processIdentify(userId, traits, options, callback){
this.userId = userId;
this.storage.setUserId(this.userId);

Expand Down Expand Up @@ -273,6 +272,120 @@ class Analytics {
}
}

identifyUser(rudderElement, callback){
this.userId = userId;
this.storage.setUserId(this.userId);

if (rudderElement && rudderElement["rl_message"]
&& rudderElement["rl_message"]["rl_context"]
&& rudderElement["rl_message"]["rl_context"]["rl_traits"] ) {
this.userTraits = traits;
this.storage.setUserTraits(this.userTraits);
}

rudderElement["rl_message"]["rl_context"]["rl_traits"] = this.userTraits;
rudderElement["rl_message"]["rl_anonymous_id"] = rudderElement[
"rl_message"
]["rl_user_id"] = rudderElement["rl_message"]["rl_context"]["rl_traits"][
"rl_anonymous_id"
] = this.userId;

console.log(JSON.stringify(rudderElement));

//try to first send to all integrations, if list populated from BE
if (this.clientIntegrationObjects) {
this.clientIntegrationObjects.forEach(obj => {
console.log("called in normal flow");
obj.identify(rudderElement);
});
}
if (!this.clientIntegrationObjects) {
console.log("pushing in replay queue");
//new event processing after analytics initialized but integrations not fetched from BE
this.toBeProcessedByIntegrationArray.push(["identify", rudderElement]);
}

// self analytics process
flush.call(this, rudderElement);

console.log("identify is called " + this.prop2);
if (callback) {
callback();
}
}

trackPage(rudderElement, callback){
if (!this.userId) {
this.userId = generateUUID();
this.storage.setUserId(this.userId);
}

rudderElement["rl_message"]["rl_context"]["rl_traits"] = this.userTraits;
rudderElement["rl_message"]["rl_anonymous_id"] = rudderElement[
"rl_message"
]["rl_user_id"] = rudderElement["rl_message"]["rl_context"]["rl_traits"][
"rl_anonymous_id"
] = this.userId;

console.log(JSON.stringify(rudderElement));

//try to first send to all integrations, if list populated from BE
if (this.clientIntegrationObjects) {
this.clientIntegrationObjects.forEach(obj => {
obj.page(rudderElement);
});
}

if (!this.clientIntegrationObjects) {
//new event processing after analytics initialized but integrations not fetched from BE
this.toBeProcessedByIntegrationArray.push(["page", rudderElement]);
}

flush.call(this, rudderElement);

console.log("page called " + this.prop1);
if (callback) {
callback();
}
}

trackEvent(rudderElement, callback){
if (!this.userId) {
this.userId = generateUUID();
this.storage.setUserId(this.userId);
}

rudderElement["rl_message"]["rl_context"]["rl_traits"] = this.userTraits;
rudderElement["rl_message"]["rl_anonymous_id"] = rudderElement[
"rl_message"
]["rl_user_id"] = rudderElement["rl_message"]["rl_context"]["rl_traits"][
"rl_anonymous_id"
] = this.userId;

console.log(JSON.stringify(rudderElement));

//try to first send to all integrations, if list populated from BE
if (this.clientIntegrationObjects) {
this.clientIntegrationObjects.forEach(obj => {
console.log("called in normal flow");
obj.track(rudderElement);
});
}
if (!this.clientIntegrationObjects) {
console.log("pushing in replay queue");
//new event processing after analytics initialized but integrations not fetched from BE
this.toBeProcessedByIntegrationArray.push(["track", rudderElement]);
}

// self analytics process
flush.call(this, rudderElement);

console.log("track is called " + this.prop2);
if (callback) {
callback();
}
}

reset() {
this.userId = "";
this.userTraits = {};
Expand All @@ -293,29 +406,23 @@ class Analytics {
let instance = new Analytics();

if (process.browser) {
//console.log("is present? " + !!window.analytics);
let eventsPushedAlready =
!!window.analytics && window.analytics.push == Array.prototype.push;

let methodArg = window.analytics ? window.analytics[0] : [];
if (methodArg.length > 0 && methodArg[0] == "load") {
instance[methodArg[0]](methodArg[1]);
//instance[methodArgNext[0]]("test args 1", "test args 2");
}

if (eventsPushedAlready) {
for (let i = 1; i < window.analytics.length; i++) {
instance.toBeProcessedArray.push(window.analytics[i]);
}

//console.log("queued " + instance.toBeProcessedArray.length);

for (let i = 0; i < instance.toBeProcessedArray.length; i++) {
let event = [...instance.toBeProcessedArray[i]];
//console.log("replay event " + event);
let method = event[0];
event.shift();
//console.log("replay event modified " + event);
instance[method](...event);
}
instance.toBeProcessedArray = [];
Expand All @@ -325,6 +432,7 @@ if (process.browser) {
let identify = instance.identify.bind(instance);
let page = instance.page.bind(instance);
let track = instance.track.bind(instance);
let trackEvent = instance.trackEvent.bind(instance);
let reset = instance.reset.bind(instance);
let load = instance.load.bind(instance);

Expand All @@ -334,6 +442,7 @@ export {
load,
identify,
reset,
trackEvent,
RudderElementBuilder,
PromotionViewedEvent,
ECommercePromotion,
Expand Down
18 changes: 13 additions & 5 deletions rudder-client-javascript/analytics/tests/html/test_browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ <h1>Page Loaded</h1>
console.log(...arguments);
analytics.push(["page", ...arguments]);
};
analytics.trackEvent = function() {
analytics.push(["trackEvent", ...arguments]);
};
analytics.track = function() {
analytics.push(["track", ...arguments]);
};
Expand All @@ -22,7 +25,7 @@ <h1>Page Loaded</h1>
};

analytics.load("1QbNPCBQp2RFWolFj2ZhXi2ER6a");
analytics.identify(
/* analytics.identify(
{
name: "Tintin",
city: "Brussels",
Expand All @@ -33,7 +36,7 @@ <h1>Page Loaded</h1>
console.log("in identify callback html");
}
);
analytics.page("Wishlist1");
analytics.page("Wishlist1"); */
/* var counter = 1
setInterval(() => {
analytics.page("Wishlist"+ ++counter)
Expand All @@ -45,7 +48,7 @@ <h1>Page Loaded</h1>
country: "Belgium",
email: "tintin@herge.com"
}, ()=>{console.log("in identify callback html")}); */
/* analytics.page("Dashboard", {
analytics.page("Dashboard", {
"title":"abc",
"url":"http://abc.com",
"path":"/abc"
Expand All @@ -54,7 +57,7 @@ <h1>Page Loaded</h1>
title: 'How to Create a Tracking Plan',
course: 'Intro to Analytics',
revenue: 10
}); */
});
</script>
<script src="../dist/browser.js"></script>

Expand All @@ -70,7 +73,12 @@ <h1>Page Loaded</h1>
course: 'Intro to Analytics',
revenue: 20
}, ()=>{console.log("in track callback html")}); */
analytics.track(
/* analytics.track('Article Completed', {
title: 'How to Create a Tracking Plan',
course: 'Intro to Analytics',
revenue: 20
}, ()=>{console.log("in track callback html")}); */
analytics.trackEvent(
new analytics.RudderElementBuilder()
.setEvent(analytics.ECommerceEvents.PROMOTION_VIEWED)
.setProperty(
Expand Down

0 comments on commit 89155bb

Please sign in to comment.