Skip to main content

Reduce Direct Traffic with Google Analytics

Written by Alexandre Dias Da Silva

Have you noticed an increase in direct traffic since you installed Axeptio?

This can be explained in the following case:

  1. A new visitor arrives on the site

  2. The widget appears, but the visitor changes page before making their choice

  3. The visitor accepts cookies on a page further along in their navigation

To calculate the source of traffic, Google Analytics will take the previous page on its first load. In a typical case, this 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 on the first page visited, the previous page no longer represents the source of traffic, but the previous page visited on the site.

Here's how to work around this problem by manually defining the source, also called the "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>

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":

Capture d'écran 2023-12-12 à 11.15.52.png

This script will store the referrer in the browser's localStorage as long as the user has not made their choice in the cookie widget.

Next, create a data layer variable named "Rf" to retrieve the information on the Google Tag Manager side:

Capture d'écran 2023-12-12 à 11.18.50.png

Finally, in your Google Analytics initialization tag, add the following parameter:

Capture d'écran 2023-12-12 à 11.23.01.png

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 to your Google Analytics initialization script:

<!-- Start of standard 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>

Did this answer your question?