Have you noticed an increase in direct traffic since you installed Axeptio?
This can be explained in the following case:
- A new visitor arrives on the site
- The widget appears, but the visitor changes the page before making their choice
- The visitor accepts cookies on a page later in their navigation
To calculate the source of traffic, Google Analytics will take the previous page at its first load. In a classic case, it could be the search engine from which the visitor arrived on the site, the advertisement that redirected them, etc.
However, if the visitor does not accept from the first page visited, the previous page no longer represents the source of the traffic, but the previous page visited on the site.
Here's how to get around this problem, by manually defining the source, also called "referrer".
You will need this script for that:
<script>
sessionStorage.setItem('referrer', sessionStorage.getItem('referrer') || document.referrer);
var rf = sessionStorage.getItem('referrer');
dataLayer = dataLayer || [];
dataLayer.push({
'Rf': rf,
});
window._axcb = window._axcb || [];
window._axcb.push(function(sdk){
sdk.on("cookies:complete", function(choices){
sessionStorage.removeItem('referrer');
});
});
</script>
This script will allow you to store the referrer in the browser's localStorage as long as the user has not made their choice in the cookie widget.
1. If you load Google Analytics via Google Tag Manager
First, load the script provided above in a new custom HTML tag, which you will trigger on "Consent Initialization - All pages":
Then, create a data layer variable named "Rf", to retrieve the information on the Google Tag Manager side:
Finally, in your Google Analytics initialization tag, add the following parameter:
2. If you load Google Analytics in your HTML
First, you will need to load the script provided above on your site.
Once this is done, add the page_referrer parameter in your Google Analytics initialization script:
<!-- Beginning of GA4 script -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-1');
gtag('set', { page_referrer: sessionStorage.getItem('referrer') }) // Line to add
</script>