Skip to content

Commit 3d2954a

Browse files
committed
Added tracking methods
1 parent a2e3d13 commit 3d2954a

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

projects/pixel/src/lib/pixel.service.ts

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { PixelConfiguration } from './pixel.models';
1+
import { PixelConfiguration, PixelEventProperties } from './pixel.models';
22
import { Inject, Injectable, Optional } from '@angular/core';
33
import { NavigationEnd, Router } from '@angular/router';
44
import { filter } from 'rxjs/operators';
55

6+
declare var fbq: any;
7+
68
@Injectable({
79
providedIn: 'root'
810
})
@@ -17,16 +19,15 @@ export class PixelService {
1719
// Log page views after router navigation ends
1820
router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(event => {
1921

20-
// if (this.isLoaded()) {
21-
// this.track('PageView');
22-
// }
22+
if (this.isLoaded()) {
23+
this.track('PageView');
24+
}
2325

24-
});
26+
});
2527
}
2628

2729
}
2830

29-
3031
/**
3132
* Initialize the Pixel tracking script
3233
* - Adds the script to page's head
@@ -43,6 +44,57 @@ export class PixelService {
4344
this.config.enabled = false;
4445
}
4546

47+
/**
48+
* Track a Standard Event as predefined by Facebook
49+
* @see {@link https://developers.facebook.com/docs/facebook-pixel/reference}
50+
* @param eventName The name of the event that is being tracked
51+
* @param properties Optional properties of the event
52+
*/
53+
track(
54+
eventName:
55+
'AddPaymentInfo' |
56+
'AddToCart' |
57+
'AddToWishlist' |
58+
'CompleteRegistration' |
59+
'Contact' |
60+
'CustomizeProduct' |
61+
'Donate' |
62+
'FindLocation' |
63+
'InitiateCheckout' |
64+
'Lead' |
65+
'PageView' |
66+
'Purchase' |
67+
'Schedule' |
68+
'Search' |
69+
'StartTrial' |
70+
'SubmitApplication' |
71+
'Subscribe' |
72+
'ViewContent',
73+
properties?: PixelEventProperties
74+
): void {
75+
76+
if (properties) {
77+
fbq('track', eventName, properties);
78+
} else {
79+
fbq('track', eventName);
80+
}
81+
82+
}
83+
84+
/**
85+
* Track a custom Event
86+
* @see {@link https://developers.facebook.com/docs/facebook-pixel/implementation/conversion-tracking#custom-conversions}
87+
* @param eventName The name of the event that is being tracked
88+
* @param properties Optional properties of the event
89+
*/
90+
trackCustom(eventName: string, properties?: object): void {
91+
if (properties) {
92+
fbq('trackCustom', eventName, properties);
93+
} else {
94+
fbq('trackCustom', eventName);
95+
}
96+
}
97+
4698
/**
4799
* Adds the Facebook Pixel tracking script to the application
48100
* @param pixelId The Facebook Pixel ID to use

0 commit comments

Comments
 (0)