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

@framit/meta-pixel

v1.0.0

Published

Simple, framework agnostic and GDPR Compliance library to track data to Meta Pixel

Downloads

468

Readme

Meta Pixel

Famework agnostic and GDPR Compliance library to track user actions to Meta Pixel.

This library is a wrapper around Meta Pixel. The library does not depend on any frameworks or libraries, and can therefore be used within React, Vue, Angular and all others JS based applications.

Get started

  npm install --save @framit/meta-pixel

or

  yarn add @framit/meta-pixel

How to use the package

First your need to initialize Pixel with your pixelId.

import Pixel, { Config } from '@framit/meta-pixel';

const config: Config = {
    pixelId: 'your-pixel-id',
    autoConfig: true, // Optional, true as default.
    isDebugMode: false; // Optional, false as default.
    disablePushState: false, // Optional, false as default. It's not recommended to disable push state, read more at https://developers.facebook.com/ads/blog/post/2017/05/29/tagging-a-single-page-application-facebook-pixel
    allowDuplicatePageViews: false // Optional, false as default.
} 

Pixel.initialize(config);

If you do not initialize Pixel before tracking, you should get an error message within the dev-tools console: “Please initialize Pixel by calling Pixel.initialize({ pixelId: 'your-pixel-id' }) before start tracking.”

Grant consent

The library does not track any kind of events to Meta Pixel before the user has granted consent to beeing tracked. To tell the library that the user has granted the consents, you need to call the setHasGrantedConsent method, Pixel.setHasGrantedConsent().

Revoke consent

When calling Pixel.revokeConsent(), the tracking will be paused until the consent is granted by the user again.

How to do tracking

Make sure you have initialized Pixel by calling Pixel.initialize({ pixelId: 'your-pixel-id' }), before tracking user actions.

import { Pixel } from '@framit/meta-pixel';

// Example of tracking add item to cart actions
Pixel.trackSingle("your-pixel-id", "AddToCart", {
    content_name: "Dont Make Me Think",
    content_category: "Books > UX",
    content_ids: ["guid-product-id"],
    product_catalog_id: "product-catalog-id"
    content_type: "product",
    value: 199.0,
    currency: "NOK",
});


// Tracking pageView based in history-state
Pixel.pageView();

// Tracking pageView based on custom eventId.
Pixel.pageView('my-awesome-event-id'); 

// Tracking Standard Events, read more at https://developers.facebook.com/docs/meta-pixel/implementation/conversion-tracking#standard-events
Pixel.track('Purchase', {
  value: 199.0,
  currency: "NOK"
});

// Tracking Standard Events, read more at https://developers.facebook.com/docs/meta-pixel/implementation/conversion-tracking#standard-events
Pixel.trackSingle('pixel-id','Purchase', {
  value: 199.0,
  currency: "NOK"
});

// Tracking Custom Events, custom events, which are visitor actions that you have defined and that you report by calling a Pixel function
Pixel.trackCustom('RecruitedAFriend', {
  value: 199.0,
  currency: "NOK"
});

// Tracking Custom Events, custom events, which are visitor actions that you have defined and that you report by calling a Pixel function
Pixel.trackSingleCustom('pixel-id','RecruitedAFriend', {
  value: 199.0,
  currency: "NOK"
});

Tip!

To make sure your setup is sending data correctly to Meta, try out Facebook Pixel Helper Chrome Extension, https://chrome.google.com/webstore/detail/facebook-pixel-helper/fdgfkebogiimcoedlicjlajpkdmockpc/related