dynamic goal values in google analytics

Scoring a goal against google is never easy. Google analytics allows you to do some strange and wonderful things, but not without some teeth grinding. I was struggling with this for a little while, and it was a great source of frustration, since there’s hardly any info out there about it. Or maybe there is lots of info, but no solution to this particular problem. I think I finally nailed it.

Dynamic Goal Conversion Values

I was trying to get some dynamic goal conversion values into Analytics. I ended up reading about Ecommerce tracking and it seemed like the way to go. Not only would I be able to pick the goal conversion value dynamically, it gives you a breakdown of each and every transaction. Very nice. After implementing it, I was quite impressed to see each transaction, product, sku etc appear neatly on the ecommerce reports. So far so good. But somehow, goals – which were set on the very same page as the ecommerce tracking code – failed to add the transaction value. The goals were tracked just fine, I could see them adding up, but not the goal value. grrrr…

I was particularly frustrated after meticulously reading through the Ecommerce Transaction Page Goals, which is very clear about this being possible, as long as you set the conversion value to zero.

The solution

There’s a small thing that doesn’t seem to get mentioned anywhere, and I’m still not sure why it causes a problem. If it doesn’t work for you, I have a few alternative approaches worth trying too…

I discovered that in my case, the google analytics code was included on the goal conversion page twice. Once, as in every page, the standard tracking code on the header. And again on the body, together with the ecommerce tracking values. Both were running fine, and as I mentioned the results appeared. But somehow it seems to create some conflict. Removing the ‘standard’ tracking section seemed to have solved this mysterious problem.

Here’s what the HTML looked like – Remove the first part

<head>
...
<!-- FIRST SECTION ON THE HEADER : TO REMOVE -->
<script>

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- DON'T REMOVE ANY FURTHER -->
</head>
<body>
...
<!-- SECOND SECTION - KEEP THIS ! -->
<script>
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']); // this tracks the page view
  _gaq.push(['_addTrans',
    '1234',           // order ID - required
    'Acme Clothing',  // affiliation or store name
    '11.99',          // total - required
    '1.29',           // tax
    '5',              // shipping
    'San Jose',       // city
    'California',     // state or province
    'USA'             // country
  ]);

   // add item might be called for every item in the shopping cart
   // where your ecommerce engine loops through each item in the cart and
   // prints out _addItem for each
  _gaq.push(['_addItem',
    '1234',           // order ID - required
    'DD44',           // SKU/code - required
    'T-Shirt',        // product name
    'Green Medium',   // category or variation
    '11.99',          // unit price - required
    '1'               // quantity - required
  ]);
  _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
...
</body>

Alternative Method

Since analytics seem to encompass lots of black magic, I’m still not 100% sure that’s the only possible solution. However, along the way I discovered some other interesting things…

Event Tracking

Instead of goal tracking by URL, you can actually track an event. If you’ve already coded the ecommerce tracking section anyway, adding event tracking to it is a doddle. Simply add this line somewhere before or after the ecommerce tracking code

_gaq.push(['_trackEvent', 'Cart', 'Checkout', 'Success', 39.99]);

In this case I defined the event category to be ‘Cart’, the action to be ‘Checkout’, the option to ‘Success’, and the value to 39.99. Once you do that, you can set your goal to use an Event instead of a URL. Here’s more on info on setting the event goals and choosing the value as the conversion value.

If your main concern is tracking conversion values, and you’re not too fussed about the whole ecommerce tracking, then this is actually a much easier way. All you need is one line of code. It can however work perfectly well together with your existing ecommerce tracking.

Wet paint – slow updates

Analytics seems to update rather slowly. It also seems to update and un-update some data if you keep refreshing. You see a new transaction. You refresh. It’s gone. Refresh again, it’s back… This is normally not a huge problem, just confusing, but when you’re trying to debug something, can make things much more difficult. For example, if you update your Goal configuration. Say you change the URL, or the tracking event, or added a new goal. It might take a good few minutes for it to update. If you trigger the goal, e.g. by going to the URL page, it might not trigger – because Analytics still uses the old goal settings. You simply have no way to find out whether the goal is active or not. You must wait for the paint to dry before you try anything new.

Other stuff

A couple of more things to watch out for:

  • The Ecommerce page says that

    For URL: Supply the URL for your shopping cart. For example: http://www.we-sell-for-you.com/mysite/myCart.asp

    In most cases, you only need to supply the relative path, e.g. /mysite/myCart.asp

  • I’m not sure whether it makes a difference, but if you’re using _gaq.push(['_setDomainName', '.domain.com']);, this might also cause some issues? (didn’t test this explicitly though)

Final grunt

Hope this is helping someone. I was pulling my hair over this so hope I can save some other people’s hairline from receding. The time it takes for the goal/ecommerce tracking to appear on analytics also makes testing it rather long and much more difficult to resolve. Having tested a few too many different combos, I’m still not 100% sure this is a bullet-proof solution, but I’m definitely seeing some goal conversion values now.

UPDATE: looks like it will only work if you also use event tracking. With URL tracking alone it doesn’t seem to pick the goal values.

5 Responses to “dynamic goal values in google analytics”

  1. Katie

    Thank you so much for this, I have been trying to figure out for ages why the e-commerce tracking works but the goal figure is 0!

  2. Czerwiu

    I’m still not able to get this work. How do you combine event tracking with URL tracking? According to your last update this is what your are actually doing right now. I see event goal value, but URL’s value is still 0.

  3. Yoav Aner

    Hi Czerwiu,

    I don’t combine them. I only use event tracking to see conversion values. You can theoretically track both URLs and events, but my goal values only show up under event tracking.

  4. radu

    Thanks for the post, just a quick clarification. If I want to track sales of same sku in different packs at different item price, should I use Product variation field ? Ex. wine sold by bottle at 15 eur / bottle and by case of 6 at 10 eur / bottle.
    Would below example be ok for a tranzaction of 61 bottles ?

    Bottle
    sku – ‘1001’
    product name – ‘wine’
    variation – ‘1’
    unit price – ’15’
    quantity – ‘1’

    Case of 6
    sku – ‘1001’
    product name – ‘wine’
    variation – ‘6’
    unit price – ’10’
    quantity – ’60’

  5. Yoav Aner

    Sorry, but I’m not sure I can answer this. My post was just talking about seeing dynamic goal values, not about the specifics of ecommerce tracking, which is rather complex on its own right.

Leave a Reply

css.php