forked from johnhidey/angular-appinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-appinsights.js
More file actions
109 lines (91 loc) · 3.36 KB
/
angular-appinsights.js
File metadata and controls
109 lines (91 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*global angular: true, appInsights: true */
(function() {
"use strict";
function appInsightProvider() {
var insights;
this.start = function(appId, appName) {
insights.start(appId, appName);
};
this.$get = function() {
insights = new insightsFactory();
return insights;
};
}
bootstrapInsights.$inject = ['$rootScope', '$route', '$location', 'insights'];
function bootstrapInsights($rootScope, $route, $location, insights) {
$rootScope.$on('$locationChangeSuccess', function() {
var pagePath;
try {
pagePath = $location.path().substr(1);
pagePath = insights.appName + '/' + pagePath;
} finally {
insights.logPageView(pagePath);
}
});
}
function insightsFactory() {
var _appId,
_appUserId,
_appName;
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length > 1) return parts.pop().split(";").shift();
return "";
}
function setCookie(name, value, days) {
var d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + "; " + expires;
}
function expireCookie(name) {
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
this.logEvent = function(event, properties, property) {
if (appInsights && _appId && appInsights.logEvent) {
appInsights.logEvent(event, properties, property);
}
if (appInsights && _appId && appInsights.trackEvent) {
appInsights.trackEvent(event, properties, property);
}
};
this.logPageView = function(page) {
if (appInsights && _appId && appInsights.logPageView) {
appInsights.logPageView(page);
}
if (appInsights && _appId && appInsights.trackPageView) {
appInsights.trackPageView(page);
}
};
this.setUserId = function(appUserId) {
_appUserId = appUserId;
if (!appInsights) {
return;
}
var currentUserId = getCookie("ai_user");
if (currentUserId !== appUserId) {
expireCookie("ai_user");
expireCookie("ai_session");
if (appInsights.context && appInsights.context.user) {
appInsights.context.user.id = appInsights.context.user.authUserId = appUserId;
}
}
};
this.start = function(appId, appName) {
_appId = appId;
_appName = appName || '(Application Root)';
if (appInsights && appId && appInsights.start) {
appInsights.start(appId);
}
if (appInsights && appId && !appInsights.start) {
appInsights = appInsights({
instrumentationKey: appId
});
}
};
}
angular.module('angular-appinsights', [])
.provider('insights', appInsightProvider)
.run(bootstrapInsights);
}());