Fix Single Page App GTM Referrer Issue
This simple guide is specifically to fixing Google Paid Search Ads referrer issue that converts google / cpc source medium into google / organic when using GTM for tracking on Single Page Apps.
If you would like to know why this issue occurs - refer to how SAP’s operate - short answer, because it’s a virtual URL path, you have to manually stamp Google Analytics through HTML to make sure the tracking doesn’t break when a user navigates to a “different page”.
Using Google Tag Manager to Fix Single Page App Attribution Issue
Step 1: Create a Custom HTML Tag
<script>window.dataLayer = window.dataLayer || []; window.dataLayer.push({ originalLocation: document.location.protocol + '//' + document.location.hostname + document.location.pathname + document.location.search }); </script>
Step 2: Locate the Page View Tag in GTM that fires on All Pages
Step 3: Add the Custom HTML Tag to the Page View Tag
Step 4: Adjust the Tag Sequence for the Custom HTML to fire first
Step 5: Create a Custom JavaScript Variable
function() { return {{Data Layer Variable - originalLocation}} || window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.search; }
Step 6: Attach the Custom Variable to the Universal Analytics under Fields to Set setting
If you are looking for a more robust implementation and have access to the SAP’s HTML:
Take the Custom HTML tag in Step 1 and implement it in the code above the GTM installation header section.
Create a Custom JavaScript Variable shown in Step 5
Once the Custom HTML & JavaScript steps are done you will need to edit every Universal Analytics tag fired through GTM; editing the following ‘Field to Set’ settings:
Field name: location
Value: {{Data Layer Variable - originalLocation}}
Field name: page
Value: {{JS - Get Page URL}}
Make sure that any queries in the URL are being passed through effectively.
Troubleshooting the Fix
When you feel confident that you’ve fixed the issue. Head over to the Analytics Dashboard and navigate to Real Time - visit the site and find yourself through the tabs, make sure that it is tracking. Grab a live ad or append ?gclid to your URL and find yourself under the google / cpc source medium. Navigate through the site and check back on the Real Time dashboard that you are not suddenly changing to google / organic.
If you need assistance - feel free to reach out to Black Drone for a quick fix.