Facebook Custom Audience pixel on SinglePageApplication SPA

扶醉桌前 提交于 2019-12-21 01:19:43

问题


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/


回答1:


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.




回答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.

<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.




回答3:


!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



来源:https://stackoverflow.com/questions/26236204/facebook-custom-audience-pixel-on-singlepageapplication-spa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!