Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Sampath-vi committed Jan 28, 2020
1 parent 6688293 commit 8046190
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 127 deletions.
84 changes: 50 additions & 34 deletions dist/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,15 @@ var rudderanalytics = (function (exports) {
};

(function () {
if (typeof window.INTERCOM === "function") {
window.INTERCOM("reattach_activator");
window.INTERCOM("update", window.intercomSettings);
var w = window;
var ic = w.Intercom;

if (typeof ic === "function") {
ic("reattach_activator");
ic("update", w.intercomSettings);
} else {
var d = document;

var i = function i() {
i.c(arguments);
};
Expand All @@ -1015,29 +1020,31 @@ var rudderanalytics = (function (exports) {
i.q.push(args);
};

window.INTERCOM = i;
w.Intercom = i;

var l = function l() {
var s = document.createElement("script");
var s = d.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = "https://widget.intercom.io/widget/" + window.intercomSettings.app_id;
var x = document.getElementsByTagName("script")[0];
var x = d.getElementsByTagName("script")[0];
x.parentNode.insertBefore(s, x);
};

if (document.readyState === "complete") {
l();
window.intercom_code = true;
} else if (window.attachEvent) {
window.attachEvent("onload", l);
} else if (w.attachEvent) {
w.attachEvent("onload", l);
window.intercom_code = true;
} else {
window.addEventListener("load", l, false);
w.addEventListener("load", l, false);
window.intercom_code = true;
}
}
})();

window.INTERCOM = Intercom;
}
}, {
key: "page",
Expand All @@ -1048,7 +1055,8 @@ var rudderanalytics = (function (exports) {
}, {
key: "identify",
value: function identify(rudderElement) {
// Create or update a user
console.log("intercom identify");
console.log(rudderElement);
var rawPayload = {};
var context = rudderElement.message.context; // identity verification

Expand Down Expand Up @@ -1090,6 +1098,8 @@ var rudderanalytics = (function (exports) {

companies.push(company);
rawPayload.companies = companies;
} else {
rawPayload[field] = context.traits[field];
}

switch (field) {
Expand All @@ -1102,54 +1112,56 @@ var rudderanalytics = (function (exports) {
break;
}
});
window.INTERCOM("update", rawPayload);
console.log(rawPayload);
window.Intercom("update", rawPayload);
}
}, {
key: "track",
value: function track(rudderElement) {
// Track events
console.log("intercom track", rudderElement);
var rawPayload = {};
var message = rudderElement.message;
var properties = message.properties ? Object.keys(message.properties) : null; // udpate properties

if (properties) {
var metadata = {
price: {},
order_number: {}
};
properties.forEach(function (property) {
var value = message.properties[property];

switch (property) {
case "price":
metadata.price["amount"] = value * 100;
break;

case "currency":
metadata.price["currency"] = value;
}

switch (property) {
case "order_ID" :
metadata.order_number["value"] = value;
break;
}
var value = message.properties[property]; // customAttributes[property] = value;
// switch (property) {
// case "price":
// metadata.price["amount"] = value * 100;
// break;
// case "currency":
// metadata.price["currency"] = value;
// default:
// break;
// }
// switch (property) {
// case "order_ID" || "order_url":
// metadata.order_number["value"] = value;
// break;
// default:
// break;
// }

rawPayload[property] = value;
});
rawPayload.metadata = metadata;
}

if (message.event) {
rawPayload.event_name = message.event;
}

rawPayload.user_id = message.userId ? message.userId : message.anonymousId;
rawPayload.created_at = Math.floor(new Date(message.originalTimestamp).getTime() / 1000); // final call to intercom
rawPayload.created_at = Math.floor(new Date(message.originalTimestamp).getTime() / 1000);
console.log("intercom end", rawPayload); // final call to intercom

window.INTERCOM("trackEvent", rawPayload);
window.Intercom("trackEvent", rawPayload.event_name, rawPayload);
}
}, {
key: "isLoaded",
value: function isLoaded() {
console.log("Intercom loaded", !!window.intercom_code);
return !!window.intercom_code;
}
}]);
Expand Down Expand Up @@ -5654,6 +5666,9 @@ var rudderanalytics = (function (exports) {
*/

function enqueue(rudderElement, type) {
console.log("loggin in enqueue");
console.log(rudderElement, type);

if (!this.eventRepository) {
this.eventRepository = eventRepository;
}
Expand Down Expand Up @@ -5866,6 +5881,7 @@ var rudderanalytics = (function (exports) {
}, {
key: "identify",
value: function identify(userId, traits, options, callback) {
console.log("in 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;
Expand Down
80 changes: 45 additions & 35 deletions integrations/INTERCOM/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,46 @@ class INTERCOM {
window.intercomSettings = {
app_id: this.APP_ID
};

(function() {
if (typeof window.INTERCOM === "function") {
window.INTERCOM("reattach_activator");
window.INTERCOM("update", window.intercomSettings);
var w = window;
var ic = w.Intercom;
if (typeof ic === "function") {
ic("reattach_activator");
ic("update", w.intercomSettings);
} else {
var d = document;
var i = function() {
i.c(arguments);
};
i.q = [];
i.c = function(args) {
i.q.push(args);
};
window.INTERCOM = i;
w.Intercom = i;
var l = function() {
var s = document.createElement("script");
var s = d.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src =
"https://widget.intercom.io/widget/" +
window.intercomSettings.app_id;
var x = document.getElementsByTagName("script")[0];
var x = d.getElementsByTagName("script")[0];
x.parentNode.insertBefore(s, x);
};
if (document.readyState === "complete") {
l();
window.intercom_code = true;
} else if (window.attachEvent) {
window.attachEvent("onload", l);
} else if (w.attachEvent) {
w.attachEvent("onload", l);
window.intercom_code = true;
} else {
window.addEventListener("load", l, false);
w.addEventListener("load", l, false);
window.intercom_code = true;
}
}
})();
window.INTERCOM = Intercom;
}

page() {
Expand All @@ -56,7 +61,8 @@ class INTERCOM {
}

identify(rudderElement) {
// Create or update a user
console.log("intercom identify");
console.log(rudderElement);

let rawPayload = {};
const context = rudderElement.message.context;
Expand Down Expand Up @@ -109,6 +115,8 @@ class INTERCOM {

companies.push(company);
rawPayload.companies = companies;
} else {
rawPayload[field] = context.traits[field];
}

switch (field) {
Expand All @@ -123,13 +131,16 @@ class INTERCOM {
break;
}
});
console.log(rawPayload);

window.INTERCOM("update", rawPayload);
}

track(rudderElement) {
// Track events

console.log("intercom track", rudderElement);

let rawPayload = {};
const message = rudderElement.message;

Expand All @@ -139,33 +150,31 @@ class INTERCOM {

// udpate properties
if (properties) {
let metadata = {
price: {},
order_number: {}
};
let customAttributes = {};

properties.forEach(property => {
const value = message.properties[property];

switch (property) {
case "price":
metadata.price["amount"] = value * 100;
break;
case "currency":
metadata.price["currency"] = value;
default:
break;
}

switch (property) {
case "order_ID" || "order_url":
metadata.order_number["value"] = value;
break;
default:
break;
}
// customAttributes[property] = value;

// switch (property) {
// case "price":
// metadata.price["amount"] = value * 100;
// break;
// case "currency":
// metadata.price["currency"] = value;
// default:
// break;
// }

// switch (property) {
// case "order_ID" || "order_url":
// metadata.order_number["value"] = value;
// break;
// default:
// break;
// }
rawPayload[property] = value;
});
rawPayload.metadata = metadata;
}

if (message.event) {
Expand All @@ -175,12 +184,13 @@ class INTERCOM {
rawPayload.created_at = Math.floor(
new Date(message.originalTimestamp).getTime() / 1000
);
console.log("intercom end", rawPayload);

// final call to intercom
window.INTERCOM("trackEvent", rawPayload);
window.INTERCOM("trackEvent", rawPayload.event_name, rawPayload);
}

isLoaded() {
console.log("Intercom loaded", !!window.intercom_code);
return !!window.intercom_code;
}
}
Expand Down

0 comments on commit 8046190

Please sign in to comment.