OPINION

Having Trouble Tracking Your Single Page Application?

by Michael Elith
06/07/2017
If you have a Single Page Application (SPA) and are using Google Analytics as your analytics provider you may have come across the issue of your marketing attribution being out of whack.
 
This is due to the way that Google Analytics attributes the marketing source to the session and the function of a SPA not reloading all of the HTML.
 

How Google Analytics attributes the marketing source

Google Analytics works on a flowchart when assigning the marketing source to the session. First looking for defined parameters such as gclid or UTM parameters to set the marketing source, and then falling back on the document referrer, and eventually if unknown it sets the channel as Direct.
 
This flowchart occurs every time that a hit (pageview, event, eccomerce) is sent to Google Analytics.
 

How Single Page Applications load

The first time you visit a single page application all of the HTML is loaded and the referrer is set. Then for each navigation through the site, the URL may change but the whole HTML does not reload and therefore the referrer does not change.
 

For example

Step1: A user clicks on a CPC advertisement and lands on the SPA where the URL and Referrer are set as:
URL: https://www.site.com/?gclid=12345
Referrer: https://www.google.com

Based on the flowchart, the gclid is seen by Google and the session is set as the Marketing Channel CPC Google
 
Step 2: The user then clicks through to another page on the SPA. The page URL changes but as the whole site does not reload and the referrer remains the same:
URL: https://www.site.com/menu
Referrer: https://www.google.com

Every time a user’s campaign source changes, Analytics opens a new session.”

As the gclid parameter is no longer in the URL, Google does not know that it is CPC Google.

Google Analytics then continues down the marketing attribution flowchart, until it gets to the document referrer, where it sees it as www.google.com. This domain is on its Search Engine list so it sets the marketing source as Organic Google and sets a new session in Google Analytics with the CPC session coming to an end.
 

So is there a solution?

A solution to this is to ensure all internal links (page changes) do not include the referrer in the Google Analytics send beacon.
 
With a standard website where the full HTML is loaded with each page change, the referrer is only set when the current hostname differs from the referrer hostname. As all internal links (page changes) on a SPA should contain the same hostname as the referrer it is not necessary to pass this variable. This is achieved by setting the variable as undefined.

Make sure that you are accurately tracking your marketing channels

Just remember the only time the referrer needs be sent is the first hit of the session.
 
If you are still having trouble tracking your Single Page Application then get in touch and we can help with ensuring your marketing channels are being attributed correctly.

FURTHER ARTICLES

Let's use data to make confident decisions