An Angular library to simplify tracking using a Facebook Pixel.
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:
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.
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 how to enable and disable ngx-pixel.
Using ngx-pixel is very simple.
You can install the NPM package with npm install ngx-pixel
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.
import { PixelModule } from 'ngx-pixel';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID' })
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
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()
.
In any component where you would like to track certain events, you can import the ngx-pixel service with import { PixelService } from 'ngx-pixel';
Then you can inject the service into your component as follows:
export class HomeComponent {
constructor(
private pixel: PixelService
) { }
}
There are two groups of events that you can track, namely Standard events and Custom events.
Standard events are common events that Facebook has created. You can find the full list here. You can track a Standard event like this:
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.:
this.pixel.track('InitiateCheckout', {
content_ids: ['ABC123', 'XYZ456'], // Item SKUs
value: 100, // Value of all items
currency: 'USD' // Currency of the value
});
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:
this.pixel.trackCustom('MyCustomEvent');
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:
this.pixel.trackCustom('MyCustomEvent', {
platform: 'limewire'
})
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.
It is still possible to initialize ngx-pixel as soon as your app module loads.
When adding ngx-pixel to app.module.ts
, add the parameter enabled: true
.
imports: [
BrowserModule,
PixelModule.forRoot({ enabled: true, pixelId: 'YOUR_PIXEL_ID'})
],
You can also enable ngx-pixel from within any of your components, like so:
export class HomeComponent {
constructor(
private pixel: PixelService
) { }
onConsent(): void {
this.pixel.initialize();
}
}
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.
Notes:
remove()
before initializing a new Pixel ID. Disabling works very similar to enabling from within a component and looks like this:
export class HomeComponent {
constructor(
private pixel: PixelService
) { }
onRevokeConsent(): void {
this.pixel.remove();
}
}
Created with ❤️ by Niels Kersic, niels.codes.