0

I've got a javascript program that is deployed as a custom HTML GTM tag that loads Javascript. The Javascript dynamically gets custom dimension index and value pairs. The following Javascript is what pushes the data to Google Analytics:

ga(function (tracker) {
  // ... code to figure out the dimension index & value ... //
  // Assuming Index & Value for custom dimension are now known 
  var dimData = {};
  dimData["dimension" + index] = value;
  dimData["nonInteraction"] = true;
  var action = "Set " + index + " to " + value;
  var trackerName = ga.getAll()[0].get('name');
  ga(trackerName + '.send', 'event', 'Dimension', action, dimData);
}

I can see Events coming through with dimension data but I'm not seeing the custom dimension data. What might be causing this?

Update: The GTM tag has Universal Analytics set to fire before it, the HTML includes an external <script> library that loads configuration (dimensions with which data to look for), parse the output and then attempts to send the data to Google Analytics. I don't think it's a race condition, its wrapped in ga(function (tracker) callback. We don't know the dimensions ahead of time.

2
  • When is your tag set to fire? Can you include screenshots of your tags and triggers? Commented Feb 10, 2017 at 16:25
  • The problem likely is because you are calling the ga object before it is actually defined due to the asynchronous nature of loading the analytics library, so you might be getting something like 'ga.getAll is not a function'. You'll either need a callback or you could set your tag to fire later like on gtm.dom. Commented Feb 11, 2017 at 16:13

1 Answer 1

1

You are likely having a race condition whereby the Ga object isn't always ready. Based on what you are doing - it looks like you might have better luck with implemeting your solution using GtM to push the data into your Account and let it handle the complexity.

Change your code above to:

    var dataLayer = dataLayer || []; // Put this in before your GTM Snippet and After the opening Body tag
// Now you are ready to work. 
// 3. Create a datalayer push for the dimensions you want to capture: 
dataLayer.push({
    'event':'addinginCustomDimData', 
    'dimension1name':'dimension1Value', 
    'dimension2name':'dimension2Value'
});

now to log the event -

  1. add a trigger using a "custom event" called "addinginCustomDimData"
  2. Add in a variable (DataLayer Type) for dimension1name and dimension2name
  3. Create a Universal Analytics event tag with your choice of category action, variable.
  4. Select custom dimension and put in the variables above in your order.

Done! Enjoy

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the pointer to using the dataLayer instead. Could I use variables "dimension1" or "dimension2" to send to Analytics? I've read in a couple places that dimensionN or metricN is used for Analytics. The issue with your suggestion is that I don't know which dimension, index or value until runtime because its reusable code meant for many websites. To use an event and variable, at design time in GTM, I don't know how to vary an index with its respective value.
As long as they are consistent within a site then you can configure the labels on the account level . Gtm just needs numbers for the dimensions so index 1 and index 2 would be your filed labels for the dimensions
Ok with respect to the labels. Maybe I misunderstand you but the index might be dimension5, dimension6 and / or dimension12. Another site would be dimension7 and dimension8. Using index 1 or index 2 would go to the wrong dimension. I don't want to create a variable for every possible index or value. Sorry if I've missed the point.
I think I know what you mean - this should work fine. Whichever way you choose to map your dimensions you can use whichever fields you prefer as this is setup on an account property level.. even for multiple sights matching multiple dimensions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.