GustavsS
GustavsS

Reputation: 1

How to track a pageview only after a user has given consent to cookies with GTM

I have set up Cookiebot for an ecommerce site and also have setup Pageviews when consent is given (which works only when the user refreshes the page or goes on to the next one). However, the page on which the user accepts his cookies isn't being tracked. Is there a way to trigger a pageview on the same page where the user accepts the cookies?

I believe that this is a common problem, but I can't find information about this online or I am searching the wrong way. Any ideas will be helpful, thanks.

Upvotes: 0

Views: 2188

Answers (2)

Kristjan
Kristjan

Reputation: 61

There are likely several ways to solve it, depending on your e-commerce setup. What we have done with the help of dataLayer pushes:

Pageview tag fires after either trigger runs:

  • Pageview event when "Cookie Consent" contains "statistics"
  • A Trigger group runs

The Trigger group consists of two events that both have to be true:

  • Pageview event when "Cookie Consent" does not contain "statistics"
  • "cookie_consent_statistics" event received

You get "Cookie Consent" variable through the "Cookiebot Consent State" variable template.

With this we solve two scenarios:

  • If the user already has given the prior consent, then this is available in the "Cookie Consent" variable. We can send the Pageview without any problems
  • If the user has not given the the consent, then the pageview event is captured, but tag is not fired. Firing the tag happens after user consents to statistics cookies and cookie_consent_statistics is pushed to dataLayer. This is possible with the Trigger Group functionality

Upvotes: 1

Ben Larson
Ben Larson

Reputation: 475

Cookiebot's documentation has this example about halfway down the "setting up Cookiebot" page:

CookiebotOnAccept The event is triggered if the user accepts the use of cookies. The event is also triggered if the user has consented at an earlier visit to the website.

Example: Load marketing cookies if the user has accepted

<script type="text/javascript">
  window.addEventListener('CookiebotOnAccept', function (e) {
    if (Cookiebot.consent.marketing) 
      {
        //Execute code that sets marketing cookies
      } 
  }, false);
</script>

You should just be able to put your GTM snippet in there and it will load when the user clicks accept, and when they've already accepted.

Upvotes: 1

Related Questions