How to push data back to Google Analytics

Since jMeter is not a real ‘Browser’ it does not execute any Javascript after receiving the response from web server. Google Analytics session tracking and number of visitors on the site is dependent on Javascript execution to send required data asynchronously back to Google, therefore when we use jMeter we are not able to see any data in Google Analytics regarding our load test (we are interested in number of visitors on the site). To overcome this problem we need to extract the required parameters from the page response and also fake some metrics then make a separate request to Google Analytics server and push the data.

Step-by-step guide

    1. GA tracks visitors bases on a unique user identifier that can be anything as long as it’s unique and each user has one. In jMeter each thread is viewed as a unique visitor, therefore if we have thread numbers #1 to thread number #20 putting load on the site with different scenarios then we can use the thread number as an identifier and push it to GA. To do this we need to do some simple coding in Java which is used by ‘BeanShell Preprocessor’ components in jMeter. My simple code to generate a uniqueId for thread is like this: 
  • import java.text.*;
    import java.util.*;  
    try {
    int threadNo = ctx.getThreadNum()+1;
    int threadGroupBase = 123456789;
    int uniqueId = threadNo + threadGroupBase;
    vars.put(“uniqueId”, Integer.toString(uniqueId));
    catch (Exception ex) {
    IsSuccess = false;
    catch (Throwable thex) {

  • As you can see it’s quite simple, where  ctx.getThreadNum() asks jMeter engine to gives us the current thread number and the rest is just adding it to a base value (see comment on base value below) and putting it inside a variable named ‘uniqueId’ which will be accessible during thread’s lifetime only to this thread.
    Note that base value should unique across thread groups (random value) but equal for all the requests within a particular thread group.

    1. Apart from that we need to have the page title to send across to GA which can be fetched from source using this RegEx extractor::
    2. At this step we have extracted the variables from the page and we can push them to GA. The request should be a GET request to${uniqueId} where ‘uniqueId’ is already generated in step 1 and the rest of parameters are:
  • Parameter Name
  • Value
  • Description
  • t
  • pageview
  • To indicate that this is a page view transaction
  • dt
  • ${documentTitle}
  • Page title which is already extracted with RegEx.
  • dl
  • ${BaseUrl}/${csvUrl}
  • Visited Page Url.
  • tid
  • UA-#######-#
  • Hardcoded to whatever value the Tracking ID / Property ID is, can be found in the Google Analytics account.
  • gtm
  • GTM Container ID. Hardcoded to whatever value the Google Tag Manager Id is.
  • v
  • 1
  • BE time including network, DNS and content download, since it’s not possible/important to calculate the exact value this can be hardcoded to anything.
  1. We are nearly done now, and just need to make sure we have RegEx extractor PostProcessors inside every request sampler and also a request sampler which is making the get request to GA right after it:

Useful Resources

Track Non-JavaScript Visits In Google Analytics


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s