Skip to content

Commit 6456041

Browse files
authored
Merge pull request TrilonIO#5 from viperguynaz/master
implemented remaining methods
2 parents d11b767 + 097cc32 commit 6456041

File tree

3 files changed

+94
-32
lines changed

3 files changed

+94
-32
lines changed

npm-debug.log.1124403154

Whitespace-only changes.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"lint": "tslint src/**/*.ts",
66
"test": "tsc && karma start",
77
"clean": "rimraf ./dist",
8-
"build": "nmp run clean && npm run tsc",
9-
"prepublish": "tsc",
8+
"build": "npm run clean && npm run tsc",
109
"tsc": "tsc"
1110
},
1211
"repository": {
@@ -24,13 +23,14 @@
2423
"main": "./dist/index.js",
2524
"dependencies": {
2625
"@angular/router": "^3.4.5",
27-
"@types/applicationinsights-js": "^0.23.5",
28-
"applicationinsights-js": "^1.0.7",
26+
"@types/applicationinsights-js": "^1.0.1",
27+
"applicationinsights-js": "^1.0.8",
2928
"rxjs": "^5.0.3"
3029
},
3130
"devDependencies": {
3231
"@angular/core": "^2.0.0",
3332
"@angular/common": "^2.0.0",
33+
"@angular/platform-browser": "^2.4.10",
3434
"codelyzer": "^0.0.28",
3535
"tslint": "^3.15.1",
3636
"typescript": "2.0.2",

src/app-insight.service.ts

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export class AppInsightsService {
1717
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
1818
// trackEvent(name: string, properties?: {[string]:string}, measurements?: {[string]:number})
1919
// Log a user action or other occurrence.
20-
trackEvent(eventName: string, eventProperties?: {string: string}, metricProperty?: {string: number}) {
21-
if (!this.isBrowser) {
20+
static trackEvent(eventName: string, eventProperties?: {[name: string]: string}, metricProperty?: {[name: string]: number}) {
21+
if (!AppInsightsService.isBrowser) {
2222
return;
2323
}
2424

@@ -33,8 +33,8 @@ export class AppInsightsService {
3333
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackpageview
3434
// trackPageView(name?: string, url?: string, properties?:{[string]:string}, measurements?: {[string]:number}, duration?: number)
3535
// Logs that a page or similar container was displayed to the user.
36-
trackPageView(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}, duration?: number) {
37-
if (!this.isBrowser) {
36+
static trackPageView(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}, duration?: number) {
37+
if (!AppInsightsService.isBrowser) {
3838
return;
3939
}
4040

@@ -50,8 +50,8 @@ export class AppInsightsService {
5050
// Starts the timer for tracking a page view. Use this instead of trackPageView if you want to control when the
5151
// page view timer starts and stops, but don't want to calculate the duration yourself. This method doesn't send any
5252
// telemetry. Call stopTrackPage to log the end of the page view and send the event.
53-
startTrackPage(name?: string) {
54-
if (!this.isBrowser) {
53+
static startTrackPage(name?: string) {
54+
if (!AppInsightsService.isBrowser) {
5555
return;
5656
}
5757

@@ -66,8 +66,8 @@ export class AppInsightsService {
6666
// stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object)
6767
// Stops the timer that was started by calling startTrackPage and sends the page view telemetry with the
6868
// specified properties and measurements. The duration of the page view will be the time between calling startTrackPage and stopTrackPage.
69-
stopTrackPage(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}) {
70-
if (!this.isBrowser) {
69+
static stopTrackPage(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}) {
70+
if (!AppInsightsService.isBrowser) {
7171
return;
7272
}
7373

@@ -78,48 +78,117 @@ export class AppInsightsService {
7878
}
7979
}
8080

81-
// [[ TODO ]] **
82-
8381
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackmetric
8482
// trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[string]:string})
8583
// Log a positive numeric value that is not associated with a specific event.
8684
// Typically used to send regular reports of performance indicators.
87-
trackMetric() { return this.notImplemented('trackMetric'); }
85+
static trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[name: string]: string}) {
86+
if (!AppInsightsService.isBrowser) {
87+
return;
88+
}
89+
90+
try {
91+
AppInsights.trackMetric(name, average, sampleCount, min, max, properties);
92+
} catch (ex) {
93+
console.warn('Angular application insights Error [trackTrace]: ', ex);
94+
}
95+
}
8896

8997
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackexception
9098
// trackException(exception: Error, handledAt?: string, properties?: {[string]:string}, measurements?: {[string]:number}, severityLevel?: AI.SeverityLevel)
9199
// Log an exception you have caught. (Exceptions caught by the browser are also logged.)
92-
trackException() { return this.notImplemented('trackException'); }
100+
static trackException(exception: Error, handledAt?: string, properties?: {[name: string]: string},
101+
measurements?: {[name: string]: number}, severityLevel?: AI.SeverityLevel) {
102+
if (!AppInsightsService.isBrowser) {
103+
return;
104+
}
105+
106+
try {
107+
AppInsights.trackException(exception, handledAt, properties, measurements, severityLevel);
108+
} catch (ex) {
109+
console.warn('Angular application insights Error [trackException]: ', ex);
110+
}
111+
}
93112

94113
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#tracktrace
95114
// trackTrace(message: string, properties?: {[string]:string}, measurements?: {[string]:number})
96115
// Log a diagnostic event such as entering or leaving a method.
97-
trackTrace() { return this.notImplemented('trackTrace'); }
116+
static trackTrace(message: string, properties?: {[name: string]: string}) {
117+
if (!AppInsightsService.isBrowser) {
118+
return;
119+
}
120+
121+
try {
122+
AppInsights.trackTrace(message, properties);
123+
} catch (ex) {
124+
console.warn('Angular application insights Error [trackTrace]: ', ex);
125+
}
126+
}
98127

99128
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackdependency
100129
// trackDependency(id: string, method: string, absoluteUrl: string, pathName: string, totalTime: number, success: boolean, resultCode: number)
101130
// Log a dependency call (for instance: ajax)
102-
trackDependency() { return this.notImplemented('trackDependency'); }
131+
static trackDependency(id: string, method: string, absoluteUrl: string, pathName: string, totalTime: number, success: boolean, resultCode: number) {
132+
if (!AppInsightsService.isBrowser) {
133+
return;
134+
}
135+
136+
try {
137+
AppInsights.trackDependency(id, method, absoluteUrl, pathName, totalTime, success, resultCode);
138+
} catch (ex) {
139+
console.warn('Angular application insights Error [trackDependency]: ', ex);
140+
}
141+
}
103142

104143
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#flush
105144
// flush()
106145
// Immediately send all queued telemetry. Synchronous.
107146
// * You don't usually have to use this, as it happens automatically on window closing.
108-
flush() { return this.notImplemented('flush'); }
147+
static flush() {
148+
if (!AppInsightsService.isBrowser) {
149+
return;
150+
}
151+
try {
152+
AppInsights.flush();
153+
} catch (ex) {
154+
console.warn('Angular application insights Error [flush]: ', ex);
155+
}
156+
157+
}
109158

110159
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#setauthenticatedusercontext
111160
// setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string)
112161
// Set the authenticated user id and the account id in this session. Use this when you have identified a specific
113162
// signed-in user. Parameters must not contain spaces or ,;=|
114-
setAuthenticatedUserContext() { return this.notImplemented('setAuthenticatedUserContext'); }
163+
static setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string) {
164+
if (!AppInsightsService.isBrowser) {
165+
return;
166+
}
167+
168+
try {
169+
AppInsights.setAuthenticatedUserContext(authenticatedUserId, accountId);
170+
} catch (ex) {
171+
console.warn('Angular application insights Error [setAuthenticatedUserContext]: ', ex);
172+
}
173+
}
115174

116175
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#clearauthenticatedusercontext
117176
// clearAuthenticatedUserContext ()
118177
// Clears the authenticated user id and the account id from the user context, and clears the associated cookie.
119-
clearAuthenticatedUserContext() { return this.notImplemented('clearAuthenticatedUserContext'); }
178+
static clearAuthenticatedUserContext() {
179+
if (!AppInsightsService.isBrowser) {
180+
return;
181+
}
182+
183+
try {
184+
AppInsights.clearAuthenticatedUserContext();
185+
} catch (ex) {
186+
console.warn('Angular application insights Error [clearAuthenticatedUserContext]: ', ex);
187+
}
188+
}
120189

121190
public init(): void {
122-
if (!this.isBrowser) {
191+
if (!AppInsightsService.isBrowser) {
123192
return;
124193
}
125194

@@ -128,27 +197,20 @@ export class AppInsightsService {
128197

129198
this.router.events.filter(event => event instanceof NavigationStart)
130199
.subscribe((event: NavigationStart) => {
131-
this.startTrackPage(event.url);
200+
AppInsightsService.startTrackPage(event.url);
132201
});
133202

134203
this.router.events.filter(event => event instanceof NavigationEnd)
135204
.subscribe((event: NavigationEnd) => {
136-
this.stopTrackPage(event.url);
205+
AppInsightsService.stopTrackPage(event.url);
137206
});
138207
} catch (ex) {
139208
console.warn('Angular application insights Error [downloadAndSetup]: ', ex);
140209
}
141210
}
142211

143-
private isBrowser(): boolean {
212+
private static isBrowser(): boolean {
144213
return (typeof (<any>window) !== undefined);
145214
}
146-
147-
private notImplemented(methodName: string) {
148-
return new Error(`
149-
This method is not implemented in ApplicationInsightsService: [ ${methodName} ]
150-
Please file any issues here: https://github.com/MarkPieszak/angular-application-insights/issues`);
151-
}
152-
153215
}
154216

0 commit comments

Comments
 (0)