File tree Expand file tree Collapse file tree 5 files changed +30
-11
lines changed Expand file tree Collapse file tree 5 files changed +30
-11
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ import { PixelModule } from 'ngx-pixel';
35
35
],
36
36
imports: [
37
37
BrowserModule ,
38
- PixelModule .forRoot ({ enabled: true , pixelId: ' YOUR_PIXEL_ID' })
38
+ PixelModule .forRoot ({ enabled: true , pixelId: ' YOUR_PIXEL_ID' })
39
39
],
40
40
providers: [],
41
41
bootstrap: [AppComponent ]
@@ -117,6 +117,12 @@ export class HomeComponent {
117
117
}
118
118
```
119
119
120
+ ## Enabling with a dynamic Pixel ID
121
+ In situations where the Pixel ID needs to be changed dynamically, this can be done using ` initialize() ` with the new Pixel ID as an optional argument.
122
+ ** Notes:**
123
+ - A Pixel ID still needs to be provided when importing *** ngx-pixel*** in the module.
124
+ - The previous instance should be removed with ` remove() ` before initializing a new Pixel ID.
125
+
120
126
## Disabling *** ngx-pixel***
121
127
Disabling works very similar to * enabling* from within a component and looks like this:
122
128
``` TypeScript
@@ -141,9 +147,8 @@ export class HomeComponent {
141
147
---
142
148
143
149
# What's next?
144
- - [ ] Checking Pixel ID's using a RegEx. First need to confirm whether all Pixel ID's follow the same format .
150
+ - [ ] Adding Angular Universal SSR support .
145
151
- [ ] Adding tests.
146
- - [ ] Testing View Engine backwards-compatibility.
147
152
- [ ] Removing all Facebook scripts on removal, not just the initial script.
148
153
149
154
---
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ import { PixelModule } from 'ngx-pixel';
35
35
],
36
36
imports: [
37
37
BrowserModule ,
38
- PixelModule .forRoot ({ enabled: true , pixelId: ' YOUR_PIXEL_ID' })
38
+ PixelModule .forRoot ({ enabled: true , pixelId: ' YOUR_PIXEL_ID' })
39
39
],
40
40
providers: [],
41
41
bootstrap: [AppComponent ]
@@ -117,6 +117,12 @@ export class HomeComponent {
117
117
}
118
118
```
119
119
120
+ ## Enabling with a dynamic Pixel ID
121
+ In situations where the Pixel ID needs to be changed dynamically, this can be done using ` initialize() ` with the new Pixel ID as an optional argument.
122
+ ** Notes:**
123
+ - A Pixel ID still needs to be provided when importing *** ngx-pixel*** in the module.
124
+ - The previous instance should be removed with ` remove() ` before initializing a new Pixel ID.
125
+
120
126
## Disabling *** ngx-pixel***
121
127
Disabling works very similar to * enabling* from within a component and looks like this:
122
128
``` TypeScript
@@ -141,16 +147,17 @@ export class HomeComponent {
141
147
---
142
148
143
149
# What's next?
144
- - [ ] Checking Pixel ID's using a RegEx. First need to confirm whether all Pixel ID's follow the same format .
150
+ - [ ] Adding Angular Universal SSR support .
145
151
- [ ] Adding tests.
146
- - [ ] Testing View Engine backwards-compatibility.
147
152
- [ ] Removing all Facebook scripts on removal, not just the initial script.
148
153
149
154
---
155
+
150
156
Created with ❤️ by Niels Kersic, [ niels.codes] ( https://niels.codes ) .
151
157
152
158
153
159
154
160
155
161
156
162
163
+
Original file line number Diff line number Diff line change @@ -3,8 +3,7 @@ import { ModuleWithProviders, NgModule } from '@angular/core';
3
3
import { PixelService } from './pixel.service' ;
4
4
5
5
@NgModule ( {
6
- imports : [
7
- ] ,
6
+ imports : [ ] ,
8
7
} )
9
8
export class PixelModule {
10
9
@@ -21,6 +20,7 @@ export class PixelModule {
21
20
22
21
/**
23
22
* Initiale the Facebook Pixel Module
23
+ *
24
24
* Add your Pixel ID as parameter
25
25
*/
26
26
static forRoot ( config : PixelConfiguration ) : ModuleWithProviders < PixelModule > {
@@ -40,12 +40,10 @@ export class PixelModule {
40
40
* @param pixelId Pixel ID to verify
41
41
*/
42
42
private static verifyPixelId ( pixelId : string ) : void {
43
-
44
43
// Have to verify first that all Pixel IDs follow the same 15 digit format
45
44
if ( pixelId === null || pixelId === undefined || pixelId . length === 0 ) {
46
45
throw Error ( 'Invalid Facebook Pixel ID. Did you pass the ID into the forRoot() function?' ) ;
47
46
}
48
-
49
47
}
50
48
51
49
}
Original file line number Diff line number Diff line change @@ -59,6 +59,10 @@ export class PixelService {
59
59
eventName : PixelEventName ,
60
60
properties ?: PixelEventProperties
61
61
) : void {
62
+ if ( ! this . isLoaded ( ) ) {
63
+ console . warn ( 'Tried to track an event without initializing a Pixel instance. Call `initialize()` first.' ) ;
64
+ return ;
65
+ }
62
66
63
67
if ( properties ) {
64
68
fbq ( 'track' , eventName , properties ) ;
@@ -76,6 +80,11 @@ export class PixelService {
76
80
* @param properties Optional properties of the event
77
81
*/
78
82
trackCustom ( eventName : string , properties ?: object ) : void {
83
+ if ( ! this . isLoaded ( ) ) {
84
+ console . warn ( 'Tried to track an event without initializing a Pixel instance. Call `initialize()` first.' ) ;
85
+ return ;
86
+ }
87
+
79
88
if ( properties ) {
80
89
fbq ( 'trackCustom' , eventName , properties ) ;
81
90
} else {
Original file line number Diff line number Diff line change 31
31
"strictInputAccessModifiers" : true ,
32
32
"strictTemplates" : true
33
33
}
34
- }
34
+ }
You can’t perform that action at this time.
0 commit comments