Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Custom Audience pixel on SinglePageApplication SPA

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/

like image 882
Michal Dymel Avatar asked Oct 07 '14 12:10

Michal Dymel


People also ask

Where do I put Facebook pixel code in Reactjs?

On initial Page Load, you should insert FB Pixel JS code into the page's header or footer. Alternately you can inject it with a Tag Manager.


3 Answers

I managed to resolve this by loading _fbq object:

<script>
    (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.
</script>

and then calling 'PixelInitialized' event every time page changes.

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

Since then facebook correctly tracks my audiences.

like image 181
Michal Dymel Avatar answered Nov 15 '22 07:11

Michal Dymel


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.

<head>
<!-- Facebook Pixel Code -->
<script>
  !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
    n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
    n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
    t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
    document,'script','//connect.facebook.net/en_US/fbevents.js');

  fbq('init', 'XXXXXXXXXX');
//fbq('track', "PageView");
</script>
<!--<noscript><img height="1" width="1" style="display:none"-->
               <!--src="https://www.facebook.com/tr?id=XXXXXXXXXX&ev=PageView&noscript=1"-->
</noscript>-->
<!-- End Facebook Pixel Code -->
</head>

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', {
        url:'/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.

like image 28
Frank Marcheski Avatar answered Nov 15 '22 07:11

Frank Marcheski


!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
    n.callMethod.apply(n,arguments):n.queue.push(arguments)};
    if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
    n.queue=[];t=b.createElement(e);t.async=!0;
    t.src=v;s=b.getElementsByTagName(e)[0];
    s.parentNode.insertBefore(t,s)}(window,document,'script',
    'https://connect.facebook.net/en_US/fbevents.js');
    // 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

like image 38
ahmed hamdy Avatar answered Nov 15 '22 06:11

ahmed hamdy