Skip to content

Commit fc32cef

Browse files
committed
Add logging
Added console warnings when either of the tracking functions is called without an active Pixel instance
1 parent 8554a5e commit fc32cef

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { PixelModule } from 'ngx-pixel';
3535
],
3636
imports: [
3737
BrowserModule,
38-
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID'})
38+
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID' })
3939
],
4040
providers: [],
4141
bootstrap: [AppComponent]
@@ -117,6 +117,12 @@ export class HomeComponent {
117117
}
118118
```
119119

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+
120126
## Disabling ***ngx-pixel***
121127
Disabling works very similar to *enabling* from within a component and looks like this:
122128
```TypeScript
@@ -141,9 +147,8 @@ export class HomeComponent {
141147
---
142148

143149
# 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.
145151
- [ ] Adding tests.
146-
- [ ] Testing View Engine backwards-compatibility.
147152
- [ ] Removing all Facebook scripts on removal, not just the initial script.
148153

149154
---

projects/pixel/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { PixelModule } from 'ngx-pixel';
3535
],
3636
imports: [
3737
BrowserModule,
38-
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID'})
38+
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID' })
3939
],
4040
providers: [],
4141
bootstrap: [AppComponent]
@@ -117,6 +117,12 @@ export class HomeComponent {
117117
}
118118
```
119119

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+
120126
## Disabling ***ngx-pixel***
121127
Disabling works very similar to *enabling* from within a component and looks like this:
122128
```TypeScript
@@ -141,16 +147,17 @@ export class HomeComponent {
141147
---
142148

143149
# 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.
145151
- [ ] Adding tests.
146-
- [ ] Testing View Engine backwards-compatibility.
147152
- [ ] Removing all Facebook scripts on removal, not just the initial script.
148153

149154
---
155+
150156
Created with ❤️ by Niels Kersic, [niels.codes](https://niels.codes).
151157

152158

153159

154160

155161

156162

163+

projects/pixel/src/lib/pixel.module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { ModuleWithProviders, NgModule } from '@angular/core';
33
import { PixelService } from './pixel.service';
44

55
@NgModule({
6-
imports: [
7-
],
6+
imports: [],
87
})
98
export class PixelModule {
109

@@ -21,6 +20,7 @@ export class PixelModule {
2120

2221
/**
2322
* Initiale the Facebook Pixel Module
23+
*
2424
* Add your Pixel ID as parameter
2525
*/
2626
static forRoot(config: PixelConfiguration): ModuleWithProviders<PixelModule> {
@@ -40,12 +40,10 @@ export class PixelModule {
4040
* @param pixelId Pixel ID to verify
4141
*/
4242
private static verifyPixelId(pixelId: string): void {
43-
4443
// Have to verify first that all Pixel IDs follow the same 15 digit format
4544
if (pixelId === null || pixelId === undefined || pixelId.length === 0) {
4645
throw Error('Invalid Facebook Pixel ID. Did you pass the ID into the forRoot() function?');
4746
}
48-
4947
}
5048

5149
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export class PixelService {
5959
eventName: PixelEventName,
6060
properties?: PixelEventProperties
6161
): void {
62+
if(!this.isLoaded()) {
63+
console.warn('Tried to track an event without initializing a Pixel instance. Call `initialize()` first.');
64+
return;
65+
}
6266

6367
if (properties) {
6468
fbq('track', eventName, properties);
@@ -76,6 +80,11 @@ export class PixelService {
7680
* @param properties Optional properties of the event
7781
*/
7882
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+
7988
if (properties) {
8089
fbq('trackCustom', eventName, properties);
8190
} else {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"strictInputAccessModifiers": true,
3232
"strictTemplates": true
3333
}
34-
}
34+
}

0 commit comments

Comments
 (0)