Facebook Custom Audience pixel on SinglePageApplication SPA

All we need is an easy explanation of the problem, so here it is.

I have a SinglePageApplication built with AngularJs. I want too add Facebook Custom Audience tracking pixel globally and update it manually every time user changes page (url).

Is it possible and if so, how?

Reference: https://developers.facebook.com/docs/reference/ads-api/custom-audience-website-faq/

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I managed to resolve this by loading _fbq object:

    (function() {
        var _fbq = window._fbq || (window._fbq = []);
        if (!_fbq.loaded) {
            var fbds = document.createElement('script');
            fbds.async = true;
            fbds.src = '//connect.facebook.net/en_US/fbds.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(fbds, s);
            _fbq.loaded = true;
        _fbq.push(['addPixelId', 'XXXXXXXXXXXXXXXXX']);
    //Notice removed 'PixelInitialized' call.

and then calling ‘PixelInitialized’ event every time page changes.

$window._fbq.push(['track', 'PixelInitialized', {}]);

Since then facebook correctly tracks my audiences.

Method 2

Here’s what I’m doing that seems to be working so far:

In my index.html file I removed the PageView function and commented out the pixel itself, as shown below.

<!-- Facebook Pixel Code -->

  fbq('init', 'XXXXXXXXXX');
//fbq('track', "PageView");
<!--<noscript><img height="1" width="1" style="display:none"-->
<!-- End Facebook Pixel Code -->

Then, I configure my app as follows…

.config(['$stateProvider', '$urlRouterProvider', '$windowProvider', function($stateProvider, $urlRouterProvider, $windowProvider) {
var $window = $windowProvider.$get();

Now, for each state change, I use the onEnter and onExit to fire off the “events” I want to capture:

.state('foo.bar', {
        templateUrl: 'template.html',
        controller: 'templateCtrl',
        onEnter: function(){
            $window.fbq('track', "Page1Enter");
        onExit: function(){
            $window.fbq('track', "Page1Exit");

I’m guessing that, if I wanted to, I could simply move any fbq('track', "PageView"); code into a controller. For my use case, though, tracking from state changes works perfectly for monitoring flow from within my app.

Hope this helps someone else.

Method 3

    // initialize facebook pixel code
    fbq('init', {SET-YOUR-ID}); // <-- replace {SET-YOUR-ID} with your facebook pixel convesion id 
    // disabled automatic push state for single web application
    fbq.disablePushState = true;
    // then call manually track PageView event
    fbq('track', 'PageView');

Into Single Page Application(SPA) you must disabled automatic listen on push State, pop State and windows history as Facebook pixel library by default hook into windows.history and push/pop state.

according to @lari answer about another related question to your question and Josh’s Post blog

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply