@textback/notification-widget
Version:
TODO: Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
89 lines (82 loc) • 3.84 kB
JavaScript
const appInsights = {
init(aiKey, userId, accountId) {
return new Promise((resolve, reject) => {
window.TextBack = window.TextBack || {};
let context = window.TextBack;
if (aiKey) {
let appInsights = context.appInsights || function (config) {
//basically most below code is got from MS so it's kinda unreadable hmm.. see MS instructions on appinsigths
function r(config) {
t[config] = function () {
let i = arguments;
t.queue.push(function () {
t[config].apply(t, i)
})
}
}
let t = {
config: config
},
u = document,
e = context,
o = "script",
s = u.createElement(o),
i, f;
for (s.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace"]; i.length;) r("track" + i.pop());
return r("setAuthenticatedUserContext"), r("clearAuthenticatedUserContext"), config.disableExceptionTracking || (i = "onerror", r("_" + i), f = e[i], e[i] = function (config, r, u, e, o) {
let s = f && f(config, r, u, e, o);
return s !== !0 && t["_" + i](config, r, u, e, o), s
}), t
}({
instrumentationKey: aiKey,
// Don't log browser exceptions.
disableExceptionTracking: true,
// Don't log ajax calls.
disableAjaxTracking: true,
//this url will point to the library - use @dev for testing when updating\editing lib - use @latest for production version of lib
//keep in mind that prod widget should always use @latest
url: '//cdn.jsdelivr.net/npm/@textback/notification-widget@latest/src/libraries/ai.1.0.11.js'
// url: '//cdn.jsdelivr.net/npm/@textback/notification-widget@dev/src/libraries/ai.1.0.11.js'
//url: '//localhost:8080/src/libraries/ai.1.0.11.js',
});
this.accountId = accountId;
context.appInsights = appInsights;
//ensure appinsights is fully loaded
let timer = setInterval(function () {
if(appInsights._onerror){
clearInterval(timer);
appInsights.setAuthenticatedUserContext(userId);
resolve(true);
}
}, 50);
}
//no aiKey - no telemetry
else{
resolve(true);
}
});
},
trackEvent(eventName, data) {
let context = window.TextBack;
context.appInsights && context.appInsights.trackEvent(eventName, data);
},
trackButtonClick(widgetId, eventName, channelType) {
let payload = {
channelType,
widgetId,
accountId: this.accountId,
widgetUrl: window.location.href
};
this.trackEvent(eventName, payload);
},
trackWidgetEvent(widgetId, eventName, reason) {
let payload = {
reason,
widgetId,
accountId: this.accountId,
widgetUrl: window.location.href
};
this.trackEvent(eventName, payload);
}
};
export default appInsights;