Sampling a percentage of your users with Google Tag Manager

by Dan on March 31, 2013

One of the neat features of Google Tag Manager is the ability to fire a tag or some custom JavaScript to only a certain percentage of your users.  Why would you want to do this?   Let’s say you want to use a test app like Optimizely but don’t want to pay for one of the higher end packages due to the traffic volume on your site.  By only firing the Optimizely tag to a percentage of your users, you’ll save yourself some dough.  Another possible use case would be a survey pop-up to 5% of our users.

To sample our users with Google Tag Manager, we’ll need to add a new tag and then a rule that fires that tag a certain percentage of the time.  My steps below assume that you will be putting this tag into a test environment first and NOT straight to production.  You don’t want to have a JavaScript alert box popping up to 10% of your users on your revenue generating site.

Let’s get to it:

  1. Log into Google Tag Manager
  2. Select the test container you want to add the sampling tag to
  3. Click ‘New Tag’
  4. Enter a name such as ’10 percent alert’ for the tag
  5. Choose a tag type of ‘custom HTML tag’
  6. In the HTML box enter your custom JavaScript code: <script>alert(‘tag fired!’);</script>
  7. Click ‘ + Add Rule to Fire Tag’
  8. Choose ‘Create new rule’
  9. Enter a rule name of ’10 percent sample’
  10. Under ‘specify the conditions’, choose ‘New Macro’
  11. Enter a macro name of ‘Random Number’
  12. Change the Macro Type to ‘Random Number’
  13. Click Save
  14. Change the ‘contains’ box to ‘ends with’
  15. Enter ’9′ in the empty value box.  (This means any time the randomly generated number ends with a 9, the tag will fire.)
  16. Click ‘Save’ to close the rule dialog
  17. Click ‘Save’ to close the tag dialog
  18. If you have a version of your test container, you’ll need to publish a new container version and then publish it.  This pushes the newly added tag to the tag container.
  19. Test!  Go to a page with your tag container and refresh it a few times.  Eventually you’ll see the JavaScript alert that says ‘tag fired!’.

If you want to change the percentage, you just need to change the rule conditions in step 15 above (be sure to change the rule name as well).  For instance, if you want to sample 1% of your users, you would change the value ‘ends with’ to ’01′ which would only match when the random number ends with ’01′ (101, 1001, etc.).  It wouldn’t match when the random number ends with ’00′,’02′, ’99′, etc.

Connect with me on Google+

  • Pingback: Muestrea un porcentaje de tus usuarios gracias a Google Tag Manager()

  • Pingback: Macro Guide For Google Tag Manager - Simo Ahava's blog()

  • Pingback: Macro Magic For Google Tag Manager - Simo Ahava's blog()

  • Pingback: Simple Split Test With Google Tag Manager - Simo Ahava's blog()

  • Addy Choi

    Extremely helpful article – thanks, Dan! However can tell me how you’d set up a firing rule for a 5% random sample?

    • danruss

      Changing the rule in step 14 to ‘matches regex’ and entering ^d+0[1-5]$ should get you very close. I haven’t had a chance to test this in GTM yet, but the idea is to match any number that ends with 01, 02, 03, 04, or 05.

      • Addy Choi

        Thanks, Dan — I’ve updated my random sample rule and we’ll see if it works!

      • Addy Choi

        I tried that firing rule but it appears to be capturing a random sample of pages — instead of capturing random sample of sessions. Any ideas how I can make this work? Thanks!

  • Nadine Hondebrink

    Great article, thanks! Do you know if the random number is session or user based? So if the same user come back to the website in a new session, is the random number the same as the previous session or does the user get a new random number? Thanks in advance.

Previous post:

Next post: