About cookies on this site

We use cookies to collect and analyse information on site performance and usage, to provide social media features and to enhance and customise content and advertisements. Learn more

Cookie settings

About cookies on this site

Cookies used on the site are categorized and below you can read about each category and allow or deny some or all of them. Learn more

Necessary

Some cookies are required to provide core functionality. The website won't function properly without these cookies and they are enabled by default and cannot be disabled.

Preferences

Preference cookies enables the web site to remember information to customize how the web site looks or behaves for each user. This may include storing selected currency, region, language or color theme.

Analytical cookies

Analytical cookies help us improve our website by collecting and reporting information on its usage.

Marketing cookies

Marketing cookies are used to track visitors across websites to allow publishers to display relevant and engaging advertisements. By enabling marketing cookies, you grant permission for personalized advertising across various platforms.

🧐 Think you know Google Analytics? Test your knowledge with our FREE GA4 skill test šŸ†

Take the test
  • Courses
    • Paid courses
    • Course bundles
    • Free courses
  • Blog
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
    • GA4 community
  • Services
  • About
    • About
    • Contact
  • Login
  • Courses
    • Paid courses
    • Course bundles
    • Free courses
  • Blog
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
    • GA4 community
  • Services
  • About
    • About
    • Contact
  • Login

December 4, 2023

Random Number in Google Tag Manager (Any Number or From Interval)

In Google Tag Manager, there is a built-in variable calledĀ RandomĀ number. As the title implies, it should return some random number, right? Well, yes. But there is more to it. In today’s blog post, I will explain how this variable works and where it is useful, and I’ll also share how to create a variable that returns a random number from a particular interval.

Where can I find this variable?

There are two places and both are available if you go to theĀ Variables section in the sidebar of the Google Tag Manager interface.

The first way is to click theĀ ConfigureĀ button and then click the checkbox next toĀ RandomĀ number.

The other one is to click theĀ NewĀ button in the User-defined variables section, then choose theĀ Random numberĀ option and save the variable.

There is no difference between these two options.

 

What does this variable do?

Simply put, theĀ Random Number variable returns a number, randomized between 0 and 2147483647.

However, it is important to note that the number is returned every time is it invoked (readĀ used). If two tags fire on the same dataLayer.pushĀ and they both use the sameĀ Random NumberĀ variable, both tags will useĀ different values. It’s because the variable was used twice: by each tag separately. And each use resulted in a different random number.

 

Where is a random number useful?

One of the examples could be to fire a tag, say, in 10% of pageviews. If a random number ends with 1, there is (almost) a 10% probability for that (because a random number can end with 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 and “ends with 1” is 1 out of 10).

Simo has mentioned it here.

Other use cases of this variable could include creating a session cookie with a random number (this number could be generated with the variable).

Subscribe and Get the Ebook - JavaScript for Google Tag Manager

A random number from an interval

But what if I want to get a random number from the interval between 1 to 3? In that case, the built-in variable becomes very limited.

The good news is that I have this little piece of JavaScript code that can be implemented as a Custom JavaScript variable.

function ()
{
 var min = 1; //edit the minimum possible value
 var max = 5; //edit the maximum possible value
 return Math.floor(Math.random()*(max-min+1)+min);
}

All you need to do is set the minimum (min) and maximum (max) value of the interval, and every time the page reloads, this variable will return a random value. In the example below, I’ve set the minimum value of 1 and maximum of 5, but you can define anything you want.

Random number from 1 to 5

 

Final words on the Random Number

And that’s the end of this fairly quick blog post. To sum up:

  • There is no difference if you use a built-in variable or create a user-defined one
  • This variable returns different values every time it is invoked. It can even be invoked multiple times during the same dataLayer.push (if multiple items are in the container, use them)
  • If you want to get a random number from a small interval, use the Custom JavaScript option instead
Julius Fedorovicius
In Google Tag Manager Tips
0 COMMENTS

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *


 

Hi, I'm Julius Fedorovicius and I'm here to help you learn Google Tag Manager and Google Analytics. Join thousands of other digital marketers and digital analysts in this exciting journey. Read more
Analytics Mania
  • Google Tag Manager Courses
  • Google Tag Manager Recipes
  • Google Tag Manager Resources
  • Google Tag Manager Community
  • Login to courses
Follow Analytics Mania
  • Subscribe to newsletter
Recent Posts
  • Scroll tracking with Google Analytics 4 (GA4 Scroll Depth Tracking)
  • Top 22 Benefits of Google Analytics 4
  • Google Tag Manager vs Google Analytics explained
Analytics Mania - Google Tag Manager and Google Analytics Blog | Privacy Policy
Manage Cookie Settings