≡ Menu

Sampling a percentage of your users with Google Tag Manager

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+

Comments on this entry are closed.