Giter VIP home page Giter VIP logo

Comments (5)

stefl avatar stefl commented on May 22, 2024 4

I've put together a sketch for this that works with my Next JS app.

let fb;
let fbLoaded = false;
export default function facebookPixelPlugin(userConfig = {}) {
  return {
    name: "facebook-ads",
    config: {
      ...userConfig,
    },
    initialize: async ({ config }) => {
      const { pixelId } = config;
      await import("react-facebook-pixel")
        .then((module) => (fb = module.default))
        .then(() => {
          if(!fbLoaded) {
            fb.init(pixelId, {
              autoConfig: true,
              debug: true,
            });
            fbLoaded = true;
          }
        });
    },
    page: ({ payload }) => {
      fb.pageView();
    },
    /* Track event */
    track: ({ payload }) => {
      console.log("facebook track", payload)
      fb.track(payload.event, payload.properties)
    },
    /* Identify user */
    identify: ({ payload }) => {
      // I believe FB doesn't have an identify API any more
    },
    loaded: () => {
      return fbLoaded;
    },
  };
}

from analytics.

jjoyce0510 avatar jjoyce0510 commented on May 22, 2024 2

Hi folks. Looks like this thread has been inactive for some time... Are there plans to productionize this?

from analytics.

DavidWells avatar DavidWells commented on May 22, 2024 1

Happy to accept a PR on this 😃

from analytics.

DavidWells avatar DavidWells commented on May 22, 2024

Hey @jasongi-actu

A fb pixel would be a great plugin 😃

Facebook PageView event doesn't let you tell it what page you're viewing, it takes it straight from the URL. It also does this automatically (even in SPAs) unless you configure disablePushStateTracking

When analytics.page() is called it uses a similar mechanism where it will grab the current page url & title.

It doesn't look like analytics standardises what you pass to identify which makes it hard to make a single identify call if you are for instance using segment and facebook which have different field names.

The data passed into the identify call is up to the application itself. The only required field is the userId

Inside of the plugin, you can manipulate/enrich the data however you'd like. Here's a plugin scaffold:

export default function facebookPixelPlugin(userConfig = {}) {
  return {
    name: 'facebook-ads',
    config: {
      ...defaultConfig,
      ...userConfig
    },
    initialize: ({ config }) => {
      // Load pixel script here
    },
    page: ({ payload }) => {
      // trigger page pixel
    },
    /* Track event */
    track: ({ payload }) => {
       // trigger custom event pixel ? idk if thats a thing 😃
    },
    /* Identify user */
    identify: ({ payload }) => {
      // Alter the payload & payload.properties however you want before 
      // triggering the pixel 
    },
    loaded: () => {
      return !!window.providerExampleLoaded
    }
  }
}

It is possible to send specific calls to specific providers like so:

// Disable sending user data to specific analytic tools
analytics.identify('xyz-123', { other: 'info' }, {
  plugins: {
    // disable sending this identify except to facebook
    all: false,
    'facebook-ads': true
  }
})

More in the docs on this here https://getanalytics.io/tutorials/sending-provider-specific-events/

from analytics.

DavidWells avatar DavidWells commented on May 22, 2024

Moved to #153

from analytics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.