0

I have a GTM container that runs on all pages on a site. This container is placed in the body tag. I am looking into a way to run a GTM HTML/JavaScript tag on 2% of all pageviews.

I see there is a random number generator and there could be a way to leverage that to accomplish what I am attempting to do, but I am not sure what parameters to use to set it up.

Any suggestions?

ahmed.eltawil
  • 561
  • 4
  • 16

2 Answers2

2

Even easier, create a Random Number variable in GTM. It returns a value between 0 and 2,147,483,647. If you want to sample 2% of visitors simply calculate it i.e. 2% of 2,147,483,647 = 42,949,672.94 or rounded 42,949,673. Then simply tell your trigger to activate if that the variable is smaller (or equal) to that number.

0

Create a variable of the custom javascript type that returns a random number in the range between 1 and 100. I took a function from here.

function() {
  max = 100;
  min = 1;
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

Name it e.g. "RandomRange".

Now create a trigger. Make it a pageview, and set as a filter "RandomRange equals or is less than 2".

The RandomRange variable will, on average, return one in 1% (one of a hundred) of all cases and two in 1% of all cases, so it'll be "two or less" in 2% of the cases. Will work only with a sufficient number of pageviews (as to get an even distribution of random numbers).

Community
  • 1
  • 1
Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62