An Angular library to simplify the use of a Facebook Pixel. It supports multiple pixelIds
npm install ngx-multi-pixel
An Angular library to simplify tracking using a Facebook Pixel. It supports multiple pixel Ids.
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:
npm install ngx-multi-pixel
app.module.ts. Make sure you use the forRoot() method. Within this method, add your Facebook Pixel ID.
typescript
import { PixelModule } from "ngx-multi-pixel";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
PixelModule.forRoot({ enabled: true, pixelId: ["YOUR_PIXEL_IDS"] }),
],
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().
3. Add it to your components
In any component where you would like to track certain events, you can import the _ngx-multi-pixel service_ with import { PixelService } from 'ngx-multi-pixel';
Then you can inject the service into your component as follows:
`TypeScript
export class HomeComponent {
constructor(
private pixel: PixelService
) { }
}
`
4. Tracking events
There are two groups of events that you can track, namely _Standard events_ and _Custom events_.
$3
Standard events are common events that Facebook has created. You can find the full list here. You can track a Standard event like this:
!Track Standard events using ngx-multi-pixel
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.:
`typescript
this.pixel.track("InitiateCheckout", {
content_ids: ["ABC123", "XYZ456"], // Item SKUs
value: 100, // Value of all items
currency: "USD", // Currency of the value
});
`
$3
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-multi-pixel_ looks like this:
`TypeScript
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:
`TypeScript
this.pixel.trackCustom('MyCustomEvent', {
platform: 'limewire'
})
`
---
Enabling and disabling _ngx-multi-pixel_
_ngx-multi-pixel_ is disabled by default. In many cases, tracking without user consent is not allowed by privacy regulations like the European GDPR. _ngx-multi-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.
Enabling _ngx-multi-pixel_ immediately
It is still possible to initialize _ngx-multi-pixel_ as soon as your app module loads.
When adding _ngx-multi-pixel_ to app.module.ts, add the parameter enabled: true.
`TypeScript
imports: [
BrowserModule,
PixelModule.forRoot({ enabled: true, pixelId: ["YOUR_PIXEL_IDS"]})
],
`
Enabling _ngx-multi-pixel_ from a component
You can also enable _ngx-multi-pixel_ from within any of your components, like so:
`TypeScript
export class HomeComponent {
constructor(
private pixel: PixelService
) { }
onConsent(): void {
this.pixel.initialize();
// this.pixel.initialize(['pixelId1', 'pixelId2',...]);
}
}
`
Enabling with a dynamic Pixel ID
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:
- A Pixel ID still needs to be provided when importing _ngx-multi-pixel_ in the module.
- The previous instance should be removed with remove() before initializing a new Pixel ID.
- This approach should not be used in combination with serverside rendering (SSR). As the module is initialized on each request, the Pixel script will default to the ID provided in the module.
Disabling _ngx-multi-pixel_
Disabling works very similar to _enabling_ from within a component and looks like this:
`TypeScript
export class HomeComponent {
constructor(
private pixel: PixelService
) { }
onRevokeConsent(): void {
this.pixel.remove();
}
}
``