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/
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.
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.
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.
!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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With