segment-integrations / analytics.js-integration-facebook-pixel

The Facebook Pixel analytics.js integration. https://segment.com/docs/integrations/facebook-pixel
MIT License
8 stars 16 forks source link

Facebook Pixel listens to pushState and automatically calls PageView events #5

Closed chiplay closed 8 years ago

chiplay commented 8 years ago

I'm not exactly sure how this should affect this Segment FB Pixel implementation, but I've noticed some duplicate PageView calls on our app and after digging around, found that fbevents.js is overwriting pushState events and calling their PageView event automatically:

(function ca() {
      if (j.disablePushState === true)
        return;
      if (!d.pushState || !d.replaceState)
        return;
      var da = function() {
        s = m;
        m = c.href;
        if (m === s)
          return;
        var ea = new v({
          allowDuplicatePageViews: true
        });
        j.trackWithoutValidation.call(ea, 'PageView');
      }
      ;
      h.injectMethod(d, 'pushState', da);
      h.injectMethod(d, 'replaceState', da);
      a.addEventListener('popstate', da, false);
    })();

Just wanted to bring that to your attention so your team can dig in further and see what is going on under the hood with FB and see if there is a better solution to getting the two to play nice together.

Thanks!

sperand-io commented 8 years ago

@chiplay thanks for reporting this! good catch. Since we implore our customers to call page in their routing handlers, we definitely will either want to noop fb.page on subsequent calls or use disable the fb lib from triggering pageview pixels automatically.

_Looks like_ we can stem these dupes by setting a disablePushState flag to true on the fbq instance on the window. However, that's not a documented feature, so I'll want to confirm it's stable with the team @ FB before implementing. Will update as soon as I hear one way or the other!