Skip to content

Commit 8e84035

Browse files
authored
v1.0.2 (#4)
* Update package.json Updated package.json to no longer require Angular 11 as a peer dependency * Fix #3 Removed pixel ID length check as these are not always the same length. There is also no documentation which shapes a pixel ID can take. * Update models Moved Pixel event names from inline to models as new type PixelEventName * Fix #1 Allow dynamic pixel IDs to be passed to `initialize()`. A default ID should still be provided in module declaration. * Update JSDoc * Add logging Added console warnings when either of the tracking functions is called without an active Pixel instance * Update package.json
1 parent 64918b0 commit 8e84035

File tree

7 files changed

+68
-47
lines changed

7 files changed

+68
-47
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/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
"url": "https://niels.codes"
88
},
99
"license": "MIT",
10-
"version": "1.0.0",
10+
"version": "1.0.2",
1111
"repository": {
1212
"type": "git",
1313
"url": "https://github.com/NielsKersic/ngx-pixel"
1414
},
1515
"peerDependencies": {
16-
"@angular/common": "^11.0.1",
17-
"@angular/core": "^11.0.1"
16+
"@angular/common": ">=11.0.1",
17+
"@angular/core": ">=11.0.1"
1818
},
1919
"dependencies": {
20-
"tslib": "^2.0.0"
20+
"tslib": ">=2.0.0"
2121
}
2222
}

projects/pixel/src/lib/pixel.models.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export interface PixelEventProperties {
5555

5656
/**
5757
* The currency for the `value` specified.
58-
* @see {@link https://developers.facebook.com/docs/marketing-api/currencies}
58+
*
59+
* See {@link https://developers.facebook.com/docs/marketing-api/currencies Facebook Pixel docs - currency codes}
5960
*/
6061
currency?:
6162
'AED' | 'ARS' | 'AUD' |
@@ -79,3 +80,22 @@ export interface PixelEventProperties {
7980
'VEF' | 'VND' |
8081
'ZAR';
8182
}
83+
84+
export type PixelEventName = 'AddPaymentInfo' |
85+
'AddToCart' |
86+
'AddToWishlist' |
87+
'CompleteRegistration' |
88+
'Contact' |
89+
'CustomizeProduct' |
90+
'Donate' |
91+
'FindLocation' |
92+
'InitiateCheckout' |
93+
'Lead' |
94+
'PageView' |
95+
'Purchase' |
96+
'Schedule' |
97+
'Search' |
98+
'StartTrial' |
99+
'SubmitApplication' |
100+
'Subscribe' |
101+
'ViewContent';

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ 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

1110
private static config: PixelConfiguration | null = null;
1211

1312
constructor( private pixel: PixelService ) {
1413
if (!PixelModule.config) {
15-
throw Error('ngx-pixel not configured correctly');
14+
throw Error('ngx-pixel not configured correctly. Pass the `pixelId` property to the `forRoot()` function');
1615
}
1716
if (PixelModule.config.enabled) {
1817
this.pixel.initialize();
@@ -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,18 +40,10 @@ export class PixelModule {
4040
* @param pixelId Pixel ID to verify
4141
*/
4242
private static verifyPixelId(pixelId: string): void {
43-
44-
// Regular expression for Pixel ID format (15 digits) (not yet implemented)
45-
const regex = /^\d{15}$/;
46-
47-
// TODO: Check validity of Pixel ID with a RegEx.
4843
// Have to verify first that all Pixel IDs follow the same 15 digit format
4944
if (pixelId === null || pixelId === undefined || pixelId.length === 0) {
5045
throw Error('Invalid Facebook Pixel ID. Did you pass the ID into the forRoot() function?');
51-
} else if (!regex.test(pixelId)) {
52-
throw Error('Invalid Facebook Pixel ID. The ID should consist of 15 digits.');
5346
}
54-
5547
}
5648

5749
}

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PixelConfiguration, PixelEventProperties } from './pixel.models';
1+
import { PixelEventName, 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';
@@ -33,9 +33,13 @@ export class PixelService {
3333
* - Adds the script to page's head
3434
* - Tracks first page view
3535
*/
36-
initialize(): void {
36+
initialize(pixelId = this.config.pixelId): void {
37+
if (this.isLoaded()) {
38+
console.warn('Tried to initialize a Pixel instance while another is already active. Please call `remove()` before initializing a new instance.');
39+
return;
40+
}
3741
this.config.enabled = true;
38-
this.addPixelScript(this.config.pixelId);
42+
this.addPixelScript(pixelId);
3943
}
4044

4145
/** Remove the Pixel tracking script */
@@ -46,32 +50,19 @@ export class PixelService {
4650

4751
/**
4852
* Track a Standard Event as predefined by Facebook
49-
* @see {@link https://developers.facebook.com/docs/facebook-pixel/reference}
53+
*
54+
* See {@link https://developers.facebook.com/docs/facebook-pixel/reference Facebook Pixel docs - reference}
5055
* @param eventName The name of the event that is being tracked
5156
* @param properties Optional properties of the event
5257
*/
5358
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',
59+
eventName: PixelEventName,
7360
properties?: PixelEventProperties
7461
): void {
62+
if(!this.isLoaded()) {
63+
console.warn('Tried to track an event without initializing a Pixel instance. Call `initialize()` first.');
64+
return;
65+
}
7566

7667
if (properties) {
7768
fbq('track', eventName, properties);
@@ -83,11 +74,17 @@ export class PixelService {
8374

8475
/**
8576
* Track a custom Event
86-
* @see {@link https://developers.facebook.com/docs/facebook-pixel/implementation/conversion-tracking#custom-conversions}
77+
*
78+
* See {@link https://developers.facebook.com/docs/facebook-pixel/implementation/conversion-tracking#custom-conversions Facebook Pixel docs - custom conversions}
8779
* @param eventName The name of the event that is being tracked
8880
* @param properties Optional properties of the event
8981
*/
9082
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+
9188
if (properties) {
9289
fbq('trackCustom', eventName, properties);
9390
} 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)