npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-multi-pixel

v2.2.2

Published

An Angular library to simplify the use of a Facebook Pixel. It supports multiple pixelIds

Downloads

1,084

Readme

ngx-multi-pixel header


Introduction

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:

  • Navigation using the Angular Router is not tracked.
  • There are no TypeScript definitions by default, so no Intellisense or linting.

ngx-multi-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-multi-pixel is disabled to make it easier to comply with GDPR and other privacy regulations. The Facebook script is only loaded after ngx-multi-pixel is enabled, which also helps cut down the initial load time of your application. Read here how to enable and disable ngx-multi-pixel.


Usage

Using ngx-multi-pixel is very simple.

1. Installing the NPM package

You can install the NPM package with npm install ngx-multi-pixel

2. Add it to your app module

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-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:

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.

Standard 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: 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.:

this.pixel.track("InitiateCheckout", {
  content_ids: ["ABC123", "XYZ456"], // Item SKUs
  value: 100, // Value of all items
  currency: "USD", // Currency of the value
});

Custom events

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:

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'
})

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.

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:

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:

export class HomeComponent {

  constructor(
    private pixel: PixelService
  ) { }

  onRevokeConsent(): void {
    this.pixel.remove();
  }

}

Important notes

  • Backwards compatibility is not guaranteed. ngx-multi-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've decided to keep using View Engine with newer Angular versions, ngx-multi-pixel might not be compatible, although I have not yet tested this to confirm.

What's next?

  • [x] Adding Angular Universal SSR support.
  • [ ] Adding tests.
  • [ ] Removing all Facebook scripts on removal, not just the initial script.

Created with ❤️ by Abhinav Akhil, inspired from niels.codes.