Limited-time sale on all Analytics Mania's courses!

Learn more
  • Courses
    • 🔥 SALE 🔥
    • Courses
    • Course bundles
  • Blog
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
    • GA4 community
  • Services
  • About
    • About
    • Contact
  • Login
  • Courses
    • 🔥 SALE 🔥
    • Courses
    • Course bundles
  • Blog
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
    • GA4 community
  • Services
  • About
    • About
    • Contact
  • Login

April 6, 2020

How to Track Pageviews with Facebook Pixel in Single Page Applications

Updated: April 6th, 2020. Here’s a situation that might look familiar to you. You land on your site and a Facebook Pixel tag fires. You continue browsing the site and the pixel never fires again (unless you hit the Refresh button in your browser).

What you’re dealing here with is called a Single Page Application and web tracking tools (including GTM) don’t behave as you would expect.

In this guide, I’ll show you how to track page views with Facebook Pixel in Single Page Applications/websites.

 

 

What is a Single Page App (SPA) / Website?

According to Google, a Single Page Application (SPA) is a web application or website that loads all of the resources required to navigate throughout the site on the first page load. As the user clicks links and interacts with the page, subsequent content is loaded dynamically (and the page technically does not reload, even though it might look differently).

The usual Facebook Pixel tracking works well with traditional websites because the snippet code is run every single time users load a new page.

However, in the single-page website/app, all of the necessary code is loaded once and the page does not reload during the entire user session. When you continue browsing your site or web app, the content is loaded dynamically. That is why your Facebook Pixel pageview fires only once. The URLs might change but that is just to give the perception of separate pages.

Luckily, everything is possible and you can track such pageviews with GTM as well. Continue reading and I’ll show you how.

 

Requirements

To get the most out of this guide, you should be already familiar with how to track Single Page Applications in general.

As for Facebook Pixel, I highly recommend that you use the Custom FB Pixel template for GTM (as described here). If you have implemented the Facebook Pixel via Custom HTML tag, make sure that the following things set up:

You have split the main FB pixel code into two parts and saved it as two separate tags:

  • FB Pixel Base code (without the fbq(‘track’, ‘PageView’);). Set this tag to fire once per page. Don’t add any trigger to it. Save it. You can also remove everything related to <noscript> (it will not work anyway).
  • Facebook Pixel pageview command surrounded by <script> tags. Set this tag to fire on All Pages. And in the Tag Sequencing section, make sure that the Facebook Pixel base tag is set as the Setup Tag.

Remember: the aforementioned two steps are required if you are using FB pixel via Custom HTML tag. If you have implemented the FB Pixel Custom Templates, you don’t need any additional configuration.

 

The tag

After you have read the Important section of this blog post, you should already have either:

  • One Pageview tag (implemented via a Custom template)
  • Or two Custom HTML tags, Base code, and Pageview command

To track the rest of the pageviews (a.k.a. Virtual Pageviews) we will be using the same tag(s).

 

The Trigger

Currently, your Facebook Pixel fires only on the Page View event in the Preview mode. That’s because you are using the All Pages trigger.

In Single Page Applications, we also need to have another trigger to track when the page URL has dynamically changed. Every time this happens, we’ll fire the Facebook Pixel Pageview once again.

I have a very extensive guide that explains how to decide which trigger to choose for your setup (because there are many nuances). So, if you haven’t yet, I highly recommend doing that.

Long story short, you have two main options:

  • History Change trigger
  • If that does not work, you should go with developer’s help & Custom Event Trigger

Let’s imagine that for me, a History Change trigger works.

Create a trigger that fires on all History Change events.

Save the trigger, enable the GTM preview mode, refresh your website and try navigating from page to page. You should see the History events every time you go to another page. If you see multiple History events on every page view, read this.

If you don’t see any History events in the preview mode, you will need to cooperate with a developer after all.

 

Add the Trigger to your tag

Add the trigger you have created in the previous chapter to your Facebook Pixel tag.

If you are using a Facebook Pixel Custom Template, just add the trigger to your Pageview tag. If you are using the Custom HTML tag, set the trigger to the tag that contains just the fbq(‘track’, ‘PageView’); command.

Make sure you are not firing the FB Base code multiple times on the same page (that will cause errors).

Every time a Facebook Pixel PageView is tracked, it will capture that current URL of the page (even if it contains # (URL Fragment)). So, you don’t need to do any additional configurations (unlike with the Google Analytics).

 

Let’s test

Now, it’s time to make sure that you have implemented everything correctly.

GTM Preview and Debug mode. In GTM, enable the Preview and Debug mode, then refresh the page where you want to install Facebook Pixel with Google Tag Manager.

At the bottom of the screen, a debug console will appear. Click the Pageview event and you should see that your Facebook Pixel tag has fired.

Then continue browsing your site. Depending on your setup, you will see History or something like virtualPageview events in the Preview mode. Click them, check whether your FB Pixel pageview tag is firing.

 

Facebook Pixel Helper. The previous tip does not mean that everything was sent to Facebook Pixel properly! There are other places we need to check. One of them is Facebook Pixel helper. Install it and you will see this icon appear in the top right corner of your Chrome browser. Once you refresh the page AND if Facebook Pixel is implemented on a page, its color will change to blue and you will see a number within that icon.

Click that icon and let’s check. We see that a PageView was tracked and there is a green checkmark icon next to it. That’s good! If there was a loader or yellow icon, that’d mean a possible problem. But a green icon is exactly what we are looking for.

If you have multiple pageviews tracked on the same page, you will see all of them as separate PageView events in the FB Pixel Helper.

 

Facebook Pixel Reports

In Facebook Business Manager, go to Events Manager > Data Sources > Select your Pixel and then Test Events.

Enter the URL of your website (if I was working with my site, I would enter https://www.analyticsmania.com) and click Open Website. After you are redirected to your own website, go back to the Test Events of your Pixel and check whether you see Pageviews coming from your own device. If nothing appears, go back to your site and refresh the page once again.

If everything is fine, this means that you have implemented Facebook Pixel in a Single Page Application correctly.

 

Learn more about how to implement Facebook Pixel with GTM

If you want to learn even more cool stuff about Facebook Pixel + GTM and make sure that everything is set up properly, enter your email address below and download my Facebook Pixel Cheat Sheet for Google Tag Manager.

 

 

 

Track page views with Facebook Pixel in single-page applications/websites: Final Words

In this fairly quick guide, you have learned what necessary steps do you need to do in order to start tracking all page views that occur in your single-page application/website.

For that, you need to fire your FB Pixel Pageview tag not only on the initial pageview (All Pages trigger) but also on subsequent virtual pageviews. Custom Event trigger or History Change trigger are great for that task.

If you have ever worked with single-page application tracking in Google Analytics, you are probably aware of the # (URL fragment) problem. Luckily, Facebook Pixel automatically tracks the # of the URL too, so you don’t need to do any additional configurations here.

 

 

Julius Fedorovicius
In Google Tag Manager Tips
17 COMMENTS
Johan
  • Mar 5 2018
  • Reply

Hello Julius,

Can the pixel track the page paths as long as we have added the History Change trigger?

Regards
Johan

    Julius Fed
    • Mar 5 2018
    • Reply

    Yes, FB pixel tracks Page Paths by default.

Nikita
  • Mar 6 2018
  • Reply

Hello Julius,

How to create "Add to Cart" and "Purchase Events" for an eCommerce website?

Best
Nikita

    Julius Fed
    • Mar 6 2018
    • Reply

    Hello, this really varies depending on a website and how it works. For example, in order to track Add to Cart events, you should start with watching this video https://www.youtube.com/watch?v=iaJ9HHvKeL8. As for Purchase events, it depends on how the payment process works. A useful resource would be this https://www.analyticsmania.com/post/google-tag-manager-form-tracking/

    It looks like you're trying to track interactions when you are not aware of GTM fundamentals and how things work. You should learn them first, here are several ideas where to start learning https://www.analyticsmania.com/post/learn-google-tag-manager/

Abbie Roberts
  • May 25 2018
  • Reply

Wait so does this work for conversions in Facebook?

    Julius Fed
    • Jun 1 2018
    • Reply

    Hey, yes. It totally can.

Bethany
  • Feb 4 2019
  • Reply

Hello,

We have a single page application where the Page View only loads once, but the URL changes as the user progresses so it seems like they are progressing from page to page. The Facebook pixel only loads on the very first page (when the Page View) loads and then drops off as soon as the URL changes. We have the trigger to fire on every history change, and I can see in GTM that the tags are firing accordingly, however the pixel is not being picked up. Any help would be much appreciated.

trevor morley
  • Feb 8 2019
  • Reply

First you are so helpful!!! I am getting unknown variable error message
"Unknown variable 'URL - Fragment' found in a tag. Edit the tag and remove the reference to the unknown variable."

Do i have to set up 'URL - Fragment' in GTM as its own variable and how do i do this? I am assuming this has to be done in user-defined variables but im not sure which one for URL - Fragment? This could be a really dumb question i apologize if it is.

    Julius Fedorovicius
    • Feb 8 2019
    • Reply

    Hey, This error means that variable {{URL - Fragment}} does not exist in your container. You need to manually create it. I have explained how to create that variable in the chapter "Two URL variables" in this blog post.

srinivas
  • Mar 19 2019
  • Reply

Hi,
iam able to trigger pageview pixel code in every page thats working good.but my requirement for one particular page. can i know how would i trigger Purchase pixel code for particular page in angular 6 application.Any help would be much appreciated.

    Julius Fedorovicius
    • Mar 20 2019
    • Reply

    How are your firing your tags on every pageview? With help of History Change trigger? If yes, then use History Change related triggers to fire your tags only on particular historyState.

Ankit Vats
  • Apr 19 2019
  • Reply

Hi
I use a react javascript app with no hash tags in url, I tried setting up history trigger but sometimes it duplicates the event.I tested with facebook pixel helper. DO you have a proper guide of setting up GTM in javascript react app with no hash tags possibility ?
Thanks

Taylor Thain
  • May 2 2019
  • Reply

If I'm understanding this right, Facebook can track virtual page views as long as there's no hash in the URL? Recently updated our site to a system that works as single page web app and am in the midst of fixing the tracking. It looks like the destination conversion is still firing even though it's this SPA.

Will this destination conversion work out of the box as long as there's no hashtag data? Am I misunderstanding this?

    Julius Fedorovicius
    • May 3 2019
    • Reply

    Facebook Pixel tracks virtual pageviews. It just does not capture the part of the URL which goes after the # (therefore, you're losing some data).

      Taylor Thain
      • May 3 2019
      • Reply

      Perfect, thanks. We don't use the # in our url structure so I think everything should be working fine.

Ilan Perez
  • May 6 2019
  • Reply

Thanks for this tutorial. It can in perfect timing for me to resolve my problem.
However I can't get it to work.
I get this error
"We detected event code but the pixel has not activated for this event, so no information was sent to Facebook. This could be due to an error in the code, but could also occur if the pixel fires on a dynamic event such as a button click.Learn more"

From the virtual pageview script.
I can tell you that before the Virtual Pageview the pixel is firing correctly.
When i view the code on the Virtual page view it shows this
fbq("trackCustom","virtualPageview",{url:"https:\/\/xxx.com\/app\/!\/home\/123\/welcome"});

which makes sense since this is the location
https://xxx.com/app/#!/home/123/welcome

any advice as to what i am doing wrong?

    Julius Fedorovicius
    • May 8 2019
    • Reply

    Why don't you insert the GTM variables in the fbq code? I'm talking about {{Page Path}} and {{URL - Fragment}}.

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *


 

Hi, I'm Julius Fedorovicius and I'm here to help you learn Google Tag Manager and Google Analytics. Join thousands of other digital marketers and digital analysts in this exciting journey. Read more
Analytics Mania
  • Google Tag Manager Courses
  • Google Tag Manager Recipes
  • Google Tag Manager Resources
  • Google Tag Manager Community
  • Login to courses
Follow Analytics Mania
  • Subscribe to newsletter
Recent Posts
  • Top 22 Benefits of Google Analytics 4
  • Track HubSpot Forms with Google Tag Manager and Google Analytics 4
  • dataLayer.push not working? Here are some reasons and solutions
Analytics Mania - Google Tag Manager and Google Analytics Blog | Privacy Policy
Manage Cookie Settings