Facebook Custom Audience pixel on SinglePageApplication SPA

前端 未结 3 477
情话喂你
情话喂你 2021-02-09 22:59

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

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-09 23:24

    !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

提交回复
热议问题