1
- import { PixelConfiguration } from './pixel.models' ;
1
+ import { PixelConfiguration , PixelEventProperties } from './pixel.models' ;
2
2
import { Inject , Injectable , Optional } from '@angular/core' ;
3
3
import { NavigationEnd , Router } from '@angular/router' ;
4
4
import { filter } from 'rxjs/operators' ;
5
5
6
+ declare var fbq : any ;
7
+
6
8
@Injectable ( {
7
9
providedIn : 'root'
8
10
} )
@@ -17,16 +19,15 @@ export class PixelService {
17
19
// Log page views after router navigation ends
18
20
router . events . pipe ( filter ( event => event instanceof NavigationEnd ) ) . subscribe ( event => {
19
21
20
- // if (this.isLoaded()) {
21
- // this.track('PageView');
22
- // }
22
+ if ( this . isLoaded ( ) ) {
23
+ this . track ( 'PageView' ) ;
24
+ }
23
25
24
- } ) ;
26
+ } ) ;
25
27
}
26
28
27
29
}
28
30
29
-
30
31
/**
31
32
* Initialize the Pixel tracking script
32
33
* - Adds the script to page's head
@@ -43,6 +44,57 @@ export class PixelService {
43
44
this . config . enabled = false ;
44
45
}
45
46
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
+
46
98
/**
47
99
* Adds the Facebook Pixel tracking script to the application
48
100
* @param pixelId The Facebook Pixel ID to use
0 commit comments