Skip to content

Instantly share code, notes, and snippets.

@pantos27
Created September 21, 2020 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pantos27/07e483ae2c076dc09f9fe746e593bdea to your computer and use it in GitHub Desktop.
Save pantos27/07e483ae2c076dc09f9fe746e593bdea to your computer and use it in GitHub Desktop.
Helper methods to use Intercom in a type script project
//https://www.intercom.com/help/en/articles/170-integrate-intercom-in-a-single-page-app
const Intercom = {
init: (appId) => {
console.log("Intercom", "init", appId);
const w = window;
const ic = w.Intercom;
w.intercomSettings = {
horizontal_padding: 24,
vertical_padding: 24,
custom_launcher_selector: ".intercom_fab",
};
if (typeof ic === "function") {
ic("reattach_activator");
ic("update", w.intercomSettings);
} else {
const d = document;
const i = function () {
i.c(arguments);
};
i.q = [];
i.c = function (args) {
i.q.push(args);
};
w.Intercom = i;
const l = function () {
const s = d.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = "https://widget.intercom.io/widget/" + appId;
const x = d.getElementsByTagName("script")[0];
x.parentNode.insertBefore(s, x);
};
if (w.attachEvent) {
w.attachEvent("onload", l);
} else {
w.addEventListener("load", l, false);
}
}
},
logout: () => {
window.Intercom("shutdown");
},
update: (data) => {
window.Intercom("update", {
last_request_at: parseInt(new Date().getTime() / 1000),
...data,
});
},
setIntercomParams: (data) => {
window.Intercom("boot", {
...data,
});
},
};
export default Intercom;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment