|
1 |
| -# Pixel |
| 1 | +# ngx-pixel |
2 | 2 |
|
3 |
| -This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.0.1. |
| 3 | + |
4 | 4 |
|
5 |
| -## Code scaffolding |
| 5 | +<center> |
| 6 | +A simple Angular library to simplify tracking using a Facebook Pixel. |
| 7 | +</center> |
6 | 8 |
|
7 |
| -Run `ng generate component component-name --project pixel` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project pixel`. |
8 |
| -> Note: Don't forget to add `--project pixel` or else it will be added to the default project in your `angular.json` file. |
| 9 | +--- |
9 | 10 |
|
10 |
| -## Build |
| 11 | +## Introduction |
| 12 | +Using a Facebook Pixel is fairly simple. You add the script to the `head` section of all pages, after which you can use the `fbq` function. However, in Angular it's not as straight-forward. The main two problems are as follows: |
11 | 13 |
|
12 |
| -Run `ng build pixel` to build the project. The build artifacts will be stored in the `dist/` directory. |
| 14 | +- Navigation using the Angular Router is not tracked. |
| 15 | +- There are no TypeScript definitions by default, so no Intellisense or linting. |
| 16 | + |
| 17 | +***ngx-pixel*** solves both of these issues. It adds a service that can be used in any component to track events and when enabled, it automatically tracks page views for each router navigation. |
| 18 | + |
| 19 | +By default, ***ngx-pixel*** is **disabled** to make it easier to comply with GDPR and other privacy regulations. The Facebook script is only loaded after ***ngx-pixel*** is enabled, which also helps cut down the initial load time of your application. Read [here](#enabling) how to *enable* and *disable* ***ngx-pixel***. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Usage |
| 24 | +Using ***ngx-pixel*** is very simple. |
| 25 | + |
| 26 | +### 1. Installing the NPM package |
| 27 | +You can install the NPM package with `npm install ngx-pixel` |
| 28 | + |
| 29 | +### 2. Add it to your app module |
| 30 | +Add the library to your app's module, usually `app.module.ts`. Make sure you use the `forRoot()` method. Within this method, add your [Facebook Pixel ID](https://www.facebook.com/business/help/952192354843755). |
| 31 | +```typescript |
| 32 | +import { PixelModule } from 'ngx-pixel'; |
| 33 | + |
| 34 | +@NgModule({ |
| 35 | + declarations: [ |
| 36 | + AppComponent |
| 37 | + ], |
| 38 | + imports: [ |
| 39 | + BrowserModule, |
| 40 | + PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID'}) |
| 41 | + ], |
| 42 | + providers: [], |
| 43 | + bootstrap: [AppComponent] |
| 44 | +}) |
| 45 | +export class AppModule { } |
| 46 | +``` |
| 47 | +**NOTE:** By default, the library does not start tracking immediately. In most cases this requires user consent to comply with GDPR and other privacy regulations. If you would still like start tracking as soon as your app module loads, include the `enabled: true` parameter in when you call `forRoot()`. |
| 48 | + |
| 49 | +### 3. Add it to your components |
| 50 | +In any component where you would like to track certain events, you can import the ***ngx-pixel service*** with `import { PixelService } from 'ngx-pixel';` |
| 51 | +Then you can inject the service into your component as follows: |
| 52 | +```TypeScript |
| 53 | +export class HomeComponent { |
| 54 | + |
| 55 | + constructor( |
| 56 | + private pixel: PixelService |
| 57 | + ) { } |
| 58 | + |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### 4. Tracking events |
| 63 | +There are two groups of events that you can track, namely *Standard events* and *Custom events*. |
| 64 | + |
| 65 | +#### Standard events |
| 66 | +**Standard events** are common events that Facebook has created. You can find the full list [here](https://developers.facebook.com/docs/facebook-pixel/reference). You can track a Standard event like this: |
| 67 | + |
| 68 | + |
| 69 | +The first argument is the type of event as defined by Facebook. The optional second argument can be used to pass more information about the event. E.g.: |
| 70 | +```typescript |
| 71 | +this.pixel.track('InitiateCheckout', { |
| 72 | + content_ids: ['ABC123', 'XYZ456'], // Item SKUs |
| 73 | + value: 100, // Value of all items |
| 74 | + currency: 'USD' // Currency of the value |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +#### Custom events |
| 79 | +Tracking **Custom events** works very similar to tracking Standard events. The only major difference is that there are no TypeScript interfaces and therefore no Intellisense. This is because the event *name* and optional *properties* can be anything. Tracking a custom event with ***ngx-pixel*** looks like this: |
| 80 | +```TypeScript |
| 81 | +this.pixel.trackCustom('MyCustomEvent'); |
| 82 | +``` |
| 83 | + |
| 84 | +And just like with Standard events, you can add more properties. This is recommended, since this enables you to create even more specific audiences within Facebook Business Manager. Which properties you add is completely up to you. Here is an example: |
| 85 | +```TypeScript |
| 86 | +this.pixel.trackCustom('MyCustomEvent', { |
| 87 | + platform: 'limewire' |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Enabling and disabling ***ngx-pixel*** <a name="enabling"></a> |
| 94 | +***ngx-pixel*** is disabled by default. In many cases, tracking without user consent is not allowed by privacy regulations like the European GDPR. ***ngx-pixel*** also doesn't inject the Facebook scripts until it is iniaitlized (upon consent), which helps cut down the initial loading size and time of your application. |
| 95 | + |
| 96 | +### Enabling ***ngx-pixel*** immediately |
| 97 | +It is still possible to initialize ***ngx-pixel*** as soon as your app module loads. |
| 98 | +When adding ***ngx-pixel*** to `app.module.ts`, add the parameter `enabled: true`. |
| 99 | +```TypeScript |
| 100 | +imports: [ |
| 101 | + BrowserModule, |
| 102 | + PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID'}) |
| 103 | +], |
| 104 | +``` |
| 105 | + |
| 106 | +### Enabling ***ngx-pixel*** from a component |
| 107 | +You can also enable ***ngx-pixel*** from within any of your components, like so: |
| 108 | +```TypeScript |
| 109 | +export class HomeComponent { |
| 110 | + |
| 111 | + constructor( |
| 112 | + private pixel: PixelService |
| 113 | + ) { } |
| 114 | + |
| 115 | + onConsent(): void { |
| 116 | + this.pixel.initialize(); |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### Disabling ***ngx-pixel*** |
| 123 | +Disabling works very similar to *enabling* from within a component and looks like this: |
| 124 | +```TypeScript |
| 125 | +export class HomeComponent { |
| 126 | + |
| 127 | + constructor( |
| 128 | + private pixel: PixelService |
| 129 | + ) { } |
| 130 | + |
| 131 | + onRevokeConsent(): void { |
| 132 | + this.pixel.remove(); |
| 133 | + } |
| 134 | + |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## Important notes |
| 141 | +- Backwards compatibility is not guaranteed. ***ngx-pixel*** was developed using Angular 11, which uses the Ivy compiler instead of the older View Engine compiler. If your project uses Angular 8 or earlier, or if you decided to keep using View Engine with newer Angular versions, ***ngx-pixel*** might not be compatible, although I have not yet tested this to confirm. |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## What's next? |
| 146 | +- [ ] Checking Pixel ID's using a RegEx. First need to confirm whether all Pixel ID's follow the same format. |
| 147 | +- [ ] Adding tests. |
| 148 | +- [ ] Testing View Engine backwards-compatibility. |
| 149 | +- [ ] Removing all Facebook scripts on removal, not just the initial script. |
| 150 | + |
| 151 | +--- |
| 152 | +<center> |
| 153 | +Created with ❤️ by Niels Kersic, [niels.codes](https://niels.codes). |
| 154 | +</center> |
13 | 155 |
|
14 |
| -## Publishing |
15 | 156 |
|
16 |
| -After building your library with `ng build pixel`, go to the dist folder `cd dist/pixel` and run `npm publish`. |
17 | 157 |
|
18 |
| -## Running unit tests |
19 | 158 |
|
20 |
| -Run `ng test pixel` to execute the unit tests via [Karma](https://karma-runner.github.io). |
21 | 159 |
|
22 |
| -## Further help |
23 | 160 |
|
24 |
| -To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page. |
|
0 commit comments