Skip to main content

Capturing existing classic GA calls with GTM – Solutions Corner #4

Adam Englebright29 January 20202 min read
Capturing existing classic GA calls with GTM – Solutions Corner #4

This is part 4 of a semi-regular series, dedicated to reusable GTM solutions for scenarios or issues we’ve encountered more than once.

The challenge with pre-GTM classic calls

Last time, we looked at how to turn on-page classic Google Analytics calls to data layer pushes, and this time we'll be looking at a supplement to that—what if the classic GA call you're looking to capture is before GTM loads? This pretty much only matters for ecommerce, so that's what the solution focuses on.

We start by defining some variables:

var gaq = window._gaq;
var ctdl_obj = {};
var ctdl_arr = [];

The second two are where we're going to push some values a bit later, and the first one is getting the existing _gaq object.

for(i=0;i < gaq.length;i++){

Now we're going to start looping through gaq to see what's in there.

 if(gaq[i][0] === "_addTrans"){
    window.ctdl_obj.id = gaq[i][1];
    window.ctdl_obj.affiliation = gaq[i][2];
    window.ctdl_obj.revenue = gaq[i][3];

If we find a "_addTrans" which is the push that adds the transaction-level information, we write it into the ctdl_obj we created at the beginning, in Enhanced Ecommerce format.

 } else if(gaq[i][0] === "_addItem"){
    window.ctdl_arr.push({
          'id':gaq[i][2],
          'name':gaq[i][3],
          'category':gaq[i][4],
		      'price':gaq[i][5],
          'quantity':gaq[i][6]
        })

Then if it's "_addItem", the pushes which add items (obviously!) we push that into the ctdl_arr array we created earlier.

 } else if(gaq[i][0] === "_trackTrans"){
    dataLayer.push({
          'event': 'hcga_trans',
            'ecommerce': {
        		'purchase':{
          			'actionField' : window.ctdl_obj,
          			'products' : window.ctdl_arr
        					}
      					}
        			}
				);
  }
}

Finally, once we hit the "_trackTrans" push, we arrange all the stuff we've gathered into EE format and push it into the data layer.

Next steps

Once you’ve added this in a custom HTML tag to fire where it's needed, you’ll need to configure a GA tag to fire on the data layer push. The solution is also available in full on the Measurelab Github. Please drop me a line if you’ve got any questions or ideas for improvement!


Suggested content

Measurelab awarded Google Cloud Marketing Analytics Specialisation

At the start of the year, if you’d asked us whether Measurelab would be standing shoulder to shoulder with Europe’s biggest consultancies by September, we would've been surprised. Not because we don't believe in ourselves, but because these things feel so distant - until suddenly, they’re not. So, here it is: we’ve been awarded the Marketing Analytics Services Partner Specialisation in Google Cloud Partner Advantage. What’s the big deal? In Google’s own words (with the obligatory Zs): “Spec

Will Hayes11 Sept 2025

BigQuery AI.GENERATE tutorial: turn SQL queries into AI-powered insights

BigQuery just got a major upgrade, you can now plug directly into Vertex AI using the new AI.GENERATE function. Translation: your analytics data and generative AI are now best friends, and they’re hanging out right inside SQL. That opens up a whole world of new analysis options for GA4 data, but it also raises some questions: * How do you actually set it up? * What’s it good for (and when should you avoid it)? * Why would you batch the query? Let’s walk through it step by step. Step 1: H

Katie Kaczmarek3 Sept 2025

How to start forecasting in BigQuery with zero training

If you’d told me five years ago that I’d be forecasting product demand using a model trained on 100 billion time points… without writing a single line of ML code… I probably would’ve asked how many coffees you’d had that day ☕️ But its a brand new world. And it’s possible. Let me explain What is TimesFM? TimesFM is a new foundation model from Google, built specifically for time-series forecasting. Think of it like GPT for time, instead of predicting the next word in a sentence, it predicts t

Katie Kaczmarek14 Jul 2025