2
votes

I am having a strange problem with Google Analytics Ecommerce tracking for which I have not been able to find a solution via my own experimentation, on this site, or on Google.

All purchases on our site occur as part of a javascript single page web app, and page reloads generally don't occur between purchases unless the user specifically visits other pages within the site. Our site uses both Google Analytics for tracking page views, and events as well as Ecommerce tracking for purchases. This has been working fine for a long time, and still does work, however one of our developers recently noticed that when making the trackTrans call, transactions and items from previous submissions to trackTrans are being re-included. This was noticed in the Google Chrome network tab (__utm requests) and has been verified using Google's own GA debug extension for Chrome.

Let me illustrate the problem using calls that are made and the log output (note that certain information has been removed for privacy reasons):

First purchase for a page load:

_gaq.push(['_addTrans', 872, (removed), 0.10, , , (removed), null, (removed)]); F3085D20782A2AC201B3D564B205E01B.cache.html:32780
_gaq.push processing "_addTrans" for args: "[872,(removed),0.10,,,(removed),,(removed)]":  [VM] ga_debug.js (4049):24

_gaq.push(['_addItem', 872, Id#11474748#GameId#17, (removed), (removed), 0.10, 1]); F3085D20782A2AC201B3D564B205E01B.cache.html:32769
_gaq.push processing "_addItem" for args: "[872,Id#11474748#GameId#17,(removed),(removed),0.10,1]":  [VM] ga_debug.js (4049):24

_gaq.push(['_trackTrans']); F3085D20782A2AC201B3D564B205E01B.cache.html:32813
_gaq.push processing "_trackTrans" for args: "[]":  [VM] ga_debug.js (4049):24

Track Transaction [VM] ga_debug.js (4049):24

Tracking beacon sent!
utmwv=5.4.4d&utms=13&utmn=1580365368&utmhn=(removed)&utmt=tran&utmtid=872&utmtst=(removed)&utmtto=0.10&utmttx=&utmtsp=&utmtci=(removed)&utmtrg=null&utmtco=(removed)&utme=8(ageGender)9(M%2065%2B)11(2)&utmcs=UTF-8&utmsr=2560x1440&utmvp=2560x1352&utmsc=24-bit&utmul=en-gb&utmje=1&utmfl=11.8%20r800&utmdt=(removed)&utmhid=1366812599&utmr=-&utmp=(removed)&utmht=1378732665904&utmac=(removed)&utmcc=__utma%3D162785085.187708818.1377079516.1378725565.1378732612.33%3B%2B__utmz%3D162785085.1377079516.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=6xCL~ [VM] ga_debug.js (4049):24

Account ID               : (removed)
Page Title               : (removed)
Host Name                : (removed)
Page                     : (removed)
Referring URL            : -
Hit ID                   : 1366812599
Hit Type                 : tran
Visitor ID               : 187708818
Session Count            : 33
Session Time - First     : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Session Time - Last      : Mon Sep 09 2013 12:19:25 GMT 0100 (BST)
Session Time - Current   : Mon Sep 09 2013 14:16:52 GMT 0100 (BST)
Campaign Time            : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Campaign Session         : 1
Campaign Count           : 1
Campaign Source          : (direct)
Campaign Medium          : (none);
Campaign Name            : (direct)
Custom Var 1             : label:'ageGender' value:'M 65 ' scope:'2'
Order ID                 : 872
Billing City             : (removed)
Billing Region           : null
Billing Country          : (removed)
Affilliation             : (removed)
Total                    : 0.10
Shipping Cost            : 
Tax                      : 
Language                 : en-gb
Encoding                 : UTF-8
Flash Version            : 11.8 r800
Java Enabled             : true
Screen Resolution        : 2560x1440
Browser Size             : 2560x1352
Color Depth              : 24-bit
Ga.js Version            : 5.4.4d
Cachebuster              : 1580365368 [VM] ga_debug.js (4049):24

Tracking beacon sent!
utmwv=5.4.4d&utms=14&utmn=2121074133&utmhn=(removed)&utmt=item&utmtid=872&utmipc=Id%2311474748%23GameId%2317&utmipn=(removed)&utmiva=(removed)&utmipr=0.10&utmiqt=1&utme=8(ageGender)9(M%2065%2B)11(2)&utmcs=UTF-8&utmsr=2560x1440&utmvp=2560x1352&utmsc=24-bit&utmul=en-gb&utmje=1&utmfl=11.8%20r800&utmdt=(removed)&utmhid=1366812599&utmr=-&utmp=(removed)&utmht=1378732665904&utmac=(removed)&utmcc=__utma%3D162785085.187708818.1377079516.1378725565.1378732612.33%3B%2B__utmz%3D162785085.1377079516.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=6xCL~ [VM] ga_debug.js (4049):24

Account ID               : (removed)
Page Title               : (removed)
Host Name                : (removed)
Page                     : (removed)
Referring URL            : -
Hit ID                   : 1366812599
Hit Type                 : item
Visitor ID               : 187708818
Session Count            : 33
Session Time - First     : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Session Time - Last      : Mon Sep 09 2013 12:19:25 GMT 0100 (BST)
Session Time - Current   : Mon Sep 09 2013 14:16:52 GMT 0100 (BST)
Campaign Time            : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Campaign Session         : 1
Campaign Count           : 1
Campaign Source          : (direct)
Campaign Medium          : (none);
Campaign Name            : (direct)
Custom Var 1             : label:'ageGender' value:'M 65 ' scope:'2'
Product Code             : Id#11474748#GameId#17
Product Name             : (removed)
Unit Price               : 0.10
Quantity                 : 1
Item Variation           : (removed)
Order ID                 : 872
Language                 : en-gb
Encoding                 : UTF-8
Flash Version            : 11.8 r800
Java Enabled             : true
Screen Resolution        : 2560x1440
Browser Size             : 2560x1352
Color Depth              : 24-bit
Ga.js Version            : 5.4.4d
Cachebuster              : 2121074133 [VM] ga_debug.js (4049):24

Second purchase without page reload (same session):

_gaq.push(['_addTrans', 873, (removed), 0.10, , , (removed), null, (removed)]); F3085D20782A2AC201B3D564B205E01B.cache.html:32780
_gaq.push processing "_addTrans" for args: "[873,(removed),0.10,,,(removed),,(removed)]":  [VM] ga_debug.js (4049):24

_gaq.push(['_addItem', 873, Id#26113584#GameId#17, (removed), (removed), 0.10, 1]); F3085D20782A2AC201B3D564B205E01B.cache.html:32769
_gaq.push processing "_addItem" for args: "[873,Id#26113584#GameId#17,(removed),(removed),0.10,1]":  [VM] ga_debug.js (4049):24

_gaq.push(['_trackTrans']); F3085D20782A2AC201B3D564B205E01B.cache.html:32813
_gaq.push processing "_trackTrans" for args: "[]":  [VM] ga_debug.js (4049):24

Track Transaction [VM] ga_debug.js (4049):24

Tracking beacon sent!
utmwv=5.4.4d&utms=20&utmn=2023058614&utmhn=(removed)&utmt=tran&utmtid=872&utmtst=(removed)&utmtto=0.10&utmttx=&utmtsp=&utmtci=(removed)&utmtrg=null&utmtco=(removed)&utme=8(ageGender)9(M%2065%2B)11(2)&utmcs=UTF-8&utmsr=2560x1440&utmvp=2560x1352&utmsc=24-bit&utmul=en-gb&utmje=1&utmfl=11.8%20r800&utmdt=(removed)&utmhid=1366812599&utmr=-&utmp=(removed)&utmht=1378733392697&utmac=(removed)&utmcc=__utma%3D162785085.187708818.1377079516.1378725565.1378732612.33%3B%2B__utmz%3D162785085.1377079516.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=6xCL~ [VM] ga_debug.js (4049):24

Account ID               : (removed)
Page Title               : (removed)
Host Name                : (removed)
Page                     : (removed)
Referring URL            : -
Hit ID                   : 1366812599
Hit Type                 : tran
Visitor ID               : 187708818
Session Count            : 33
Session Time - First     : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Session Time - Last      : Mon Sep 09 2013 12:19:25 GMT 0100 (BST)
Session Time - Current   : Mon Sep 09 2013 14:16:52 GMT 0100 (BST)
Campaign Time            : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Campaign Session         : 1
Campaign Count           : 1
Campaign Source          : (direct)
Campaign Medium          : (none);
Campaign Name            : (direct)
Custom Var 1             : label:'ageGender' value:'M 65 ' scope:'2'
Order ID                 : 872
Billing City             : (removed)
Billing Region           : null
Billing Country          : (removed)
Affilliation             : (removed)
Total                    : 0.10
Shipping Cost            : 
Tax                      : 
Language                 : en-gb
Encoding                 : UTF-8
Flash Version            : 11.8 r800
Java Enabled             : true
Screen Resolution        : 2560x1440
Browser Size             : 2560x1352
Color Depth              : 24-bit
Ga.js Version            : 5.4.4d
Cachebuster              : 2023058614 [VM] ga_debug.js (4049):24

Tracking beacon sent!
utmwv=5.4.4d&utms=21&utmn=428191029&utmhn=(removed)&utmt=item&utmtid=872&utmipc=Id%2311474748%23GameId%2317&utmipn=(removed)&utmiva=(removed)&utmipr=0.10&utmiqt=1&utme=8(ageGender)9(M%2065%2B)11(2)&utmcs=UTF-8&utmsr=2560x1440&utmvp=2560x1352&utmsc=24-bit&utmul=en-gb&utmje=1&utmfl=11.8%20r800&utmdt=(removed)&utmhid=1366812599&utmr=-&utmp=(removed)&utmht=1378733392698&utmac=(removed)&utmcc=__utma%3D162785085.187708818.1377079516.1378725565.1378732612.33%3B%2B__utmz%3D162785085.1377079516.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=6xCL~ [VM] ga_debug.js (4049):24

Account ID               : (removed)
Page Title               : (removed)
Host Name                : (removed)
Page                     : (removed)
Referring URL            : -
Hit ID                   : 1366812599
Hit Type                 : item
Visitor ID               : 187708818
Session Count            : 33
Session Time - First     : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Session Time - Last      : Mon Sep 09 2013 12:19:25 GMT 0100 (BST)
Session Time - Current   : Mon Sep 09 2013 14:16:52 GMT 0100 (BST)
Campaign Time            : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Campaign Session         : 1
Campaign Count           : 1
Campaign Source          : (direct)
Campaign Medium          : (none);
Campaign Name            : (direct)
Custom Var 1             : label:'ageGender' value:'M 65 ' scope:'2'
Product Code             : Id#11474748#GameId#17
Product Name             : (removed)
Unit Price               : 0.10
Quantity                 : 1
Item Variation           : (removed)
Order ID                 : 872
Language                 : en-gb
Encoding                 : UTF-8
Flash Version            : 11.8 r800
Java Enabled             : true
Screen Resolution        : 2560x1440
Browser Size             : 2560x1352
Color Depth              : 24-bit
Ga.js Version            : 5.4.4d
Cachebuster              : 428191029 [VM] ga_debug.js (4049):24

Tracking beacon sent!
utmwv=5.4.4d&utms=22&utmn=2054976501&utmhn=(removed)&utmt=tran&utmtid=873&utmtst=(removed)&utmtto=0.10&utmttx=&utmtsp=&utmtci=(removed)&utmtrg=null&utmtco=(removed)&utme=8(ageGender)9(M%2065%2B)11(2)&utmcs=UTF-8&utmsr=2560x1440&utmvp=2560x1352&utmsc=24-bit&utmul=en-gb&utmje=1&utmfl=11.8%20r800&utmdt=(removed)&utmhid=1366812599&utmr=-&utmp=(removed)&utmht=1378733392698&utmac=(removed)&utmcc=__utma%3D162785085.187708818.1377079516.1378725565.1378732612.33%3B%2B__utmz%3D162785085.1377079516.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=6xCL~ [VM] ga_debug.js (4049):24

Account ID               : (removed)
Page Title               : (removed)
Host Name                : (removed)
Page                     : (removed)
Referring URL            : -
Hit ID                   : 1366812599
Hit Type                 : tran
Visitor ID               : 187708818
Session Count            : 33
Session Time - First     : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Session Time - Last      : Mon Sep 09 2013 12:19:25 GMT 0100 (BST)
Session Time - Current   : Mon Sep 09 2013 14:16:52 GMT 0100 (BST)
Campaign Time            : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Campaign Session         : 1
Campaign Count           : 1
Campaign Source          : (direct)
Campaign Medium          : (none);
Campaign Name            : (direct)
Custom Var 1             : label:'ageGender' value:'M 65 ' scope:'2'
Order ID                 : 873
Billing City             : (removed)
Billing Region           : null
Billing Country          : (removed)
Affilliation             : (removed)
Total                    : 0.10
Shipping Cost            : 
Tax                      : 
Language                 : en-gb
Encoding                 : UTF-8
Flash Version            : 11.8 r800
Java Enabled             : true
Screen Resolution        : 2560x1440
Browser Size             : 2560x1352
Color Depth              : 24-bit
Ga.js Version            : 5.4.4d
Cachebuster              : 2054976501 [VM] ga_debug.js (4049):24

Tracking beacon sent!
utmwv=5.4.4d&utms=23&utmn=900028249&utmhn=(removed)&utmt=item&utmtid=873&utmipc=Id%2326113584%23GameId%2317&utmipn=(removed)&utmiva=(removed)&utmipr=0.10&utmiqt=1&utme=8(ageGender)9(M%2065%2B)11(2)&utmcs=UTF-8&utmsr=2560x1440&utmvp=2560x1352&utmsc=24-bit&utmul=en-gb&utmje=1&utmfl=11.8%20r800&utmdt=(removed)&utmhid=1366812599&utmr=-&utmp=(removed)&utmht=1378733392699&utmac=(removed)&utmcc=__utma%3D162785085.187708818.1377079516.1378725565.1378732612.33%3B%2B__utmz%3D162785085.1377079516.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=6xCL~ [VM] ga_debug.js (4049):24

Account ID               : (removed)
Page Title               : (removed)
Host Name                : (removed)
Page                     : (removed)
Referring URL            : -
Hit ID                   : 1366812599
Hit Type                 : item
Visitor ID               : 187708818
Session Count            : 33
Session Time - First     : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Session Time - Last      : Mon Sep 09 2013 12:19:25 GMT 0100 (BST)
Session Time - Current   : Mon Sep 09 2013 14:16:52 GMT 0100 (BST)
Campaign Time            : Wed Aug 21 2013 11:05:16 GMT 0100 (BST)
Campaign Session         : 1
Campaign Count           : 1
Campaign Source          : (direct)
Campaign Medium          : (none);
Campaign Name            : (direct)
Custom Var 1             : label:'ageGender' value:'M 65 ' scope:'2'
Product Code             : Id#26113584#GameId#17
Product Name             : (removed)
Unit Price               : 0.10
Quantity                 : 1
Item Variation           : (removed)
Order ID                 : 873
Language                 : en-gb
Encoding                 : UTF-8
Flash Version            : 11.8 r800
Java Enabled             : true
Screen Resolution        : 2560x1440
Browser Size             : 2560x1352
Color Depth              : 24-bit
Ga.js Version            : 5.4.4d
Cachebuster              : 900028249 [VM] ga_debug.js (4049):24

For the first purchase two tracking beacons are sent:

  • one of Hit Type "tran" with an Order ID of "872"
  • one of Hit Type "item" with an Order ID of "872" and a Product Code (SKU) of "Id#11474748#GameId#17"

This is correct.

For the second purchase four tracking beacons are sent:

  • one of Hit Type "tran" with an Order ID of "872" (the previous transaction)
  • one of Hit Type "item" with an Order ID of "872" and a Product Code (SKU) of "Id#11474748#GameId#17" (the previous item)
  • one of Hit Type "tran" with an Order ID of "873" (the new transaction)
  • one of Hit Type "item" with an Order ID of "873" and a Product Code (SKU) of "Id#26113584#GameId#17" (the new item)

While the latter tracking beacons are correct, the first two are duplicates of the first purchase, which is wrong.

Subsequent purchases would see 6, 8, 10 etc tracking beacons sent, where the first beacons are from all of the previous purchases, not just the most recent.

Product Codes (SKUs) are unique for all purchases by definition within our application. Our code has been verified to not be making extra javascript ecommerce tracking calls. This has also been verified by executing the javascript ecommerce calls directly in the Chrome console, and seeing the same issue of previous transactions being re-submitted as part of the latest transaction, seemingly independent of our application.

We have verified in our production analytics that there are duplicate purchases (more than one purchase with the same SKU) so this is affecting our reporting (thankfully the majority of our financial reporting is done via reports from our database rather than Google Analytics).

Also note that page views are definitely being tracked before ecommerce tracking code is executed as specified in the docs!

Any help identifying the issue would be much appreciated!

--UPDATE

I have reproduced the issue with a simple test case running outside of our application using the html below (Google Analytics Account ID will need to be entered before running, I ran the file from a simple apache server):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=9;IE=8"/>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <meta name="viewport" content="maximum-scale=1.0, width=device-width"/>

    <script type="text/javascript">
        /*<![CDATA[*/
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); /* REPLACE WITH YOUR OWN ACCOUNT ID */
        _gaq.push(['_setDomainName', 'none']); /* Must be set to none when running from localhost, but normally set to auto in Prod */
        _gaq.push(['_setAllowLinker', false]); /* set to true in Prod but doesn't seem to have any effect on the issue */

        (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>

    <script type="text/javascript">
        /*<![CDATA[*/
        var orderId = 1;
        var sku = 200;
        makePurchase = function() {
            console.error("...NEW PURCHASE HAPPENING..."); /* Error level just so it's easy to see in the console when a new purchase starts */
            _gaq.push(['_trackPageview', '/purchase/complete']);
            console.log("Tracking purchase for order with id " + orderId + " and SKU " + sku);
            _gaq.push(['_addTrans', orderId, 'Acme Clothing', '11.99', '1.29' , '5', 'San Joes', 'California', 'USA']);
            _gaq.push(['_addItem', orderId, sku, 'T-Shirt', 'Green Medium', '11.99', '1']);
            _gaq.push(['_trackTrans']);
            incrementOrderIds();
        }

        var incrementOrderIds = function() {
            orderId = orderId + 1;
            sku = sku + 10;
        }
        /*]]>*/
    </script>

    <title>Google Analytics Ecommerce Test Page</title>
</head>

<body>

    <h1>Google Analytics Ecommerce Test</h1>
    <div>
        <a href="#" onclick="makePurchase()">Make Purchase</a>
    </div>

</body>

</html>
2
It is an undocumented feature, but take a look at using _clearTrans after _trackTrans. It'll basically remove the previous transaction information. I'd strongly recommend testing this out in a test environment before utilizing it in a production env, but local tests show for me that it produces the intended behavior you're looking for. - MisterPhilip
Thanks! It certainly appears to do what I need, and of course the fix will go through our usual test environments before making it to prod. Please can you write your response as an answer to the question so you can receive the reputation bounty. - pauln

2 Answers

4
votes

As I mentioned in my original comment, this is an undocumented feature, but take a look at using _clearTrans after _trackTrans. As the name suggests it clears out all of the current ecommerce data (items and transactions).

Modifying your example would end up looking like this..:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); /* REPLACE WITH YOUR OWN ACCOUNT ID */
_gaq.push(['_setDomainName', 'none']); /* Must be set to none when running from localhost, but normally set to auto in Prod */
_gaq.push(['_setAllowLinker', false]); /* set to true in Prod but doesn't seem to have any effect on the issue */

(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/u/ga_debug.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

var orderId = 1;
var sku = 200;
makePurchase = function() {
    console.group('New Purchase');
    _gaq.push(['_trackPageview', '/purchase/complete']);
    console.log("Tracking purchase for order with id " + orderId + " and SKU " + sku);
    _gaq.push(['_addTrans', orderId, 'Acme Clothing', '11.99', '1.29' , '5', 'San Joes', 'California', 'USA']);
    _gaq.push(['_addItem', orderId, sku, 'T-Shirt', 'Green Medium', '11.99', '1']);
    _gaq.push(['_trackTrans']);
    _gaq.push(['_clearTrans']); // Clear Transaction data
    incrementOrderIds();
    console.groupEnd();
}

var incrementOrderIds = function() {
    orderId = orderId + 1;
    sku = sku + 10;
}

Also, if / when you end up switching to Universal Analytics, the fine folks at Google were nice enough to include this via ga('ecommerce:clear') and as a bonus... they even documented it!

0
votes

_addTrans creates a transaction object with the given values. As with _addItem(), this method handles only transaction tracking and provides no additional ecommerce functionality. Therefore, if the transaction is a duplicate of an existing transaction for that session, the old transaction values are over-written with the new transaction values. Arguments for this method are matched by position, so be sure to supply all parameters, even if some of them have an empty value.

Source: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce

_addTrans(transactionId, affiliation, total, tax, shipping, city, state, country)

Since you are in the same session every time you push a trans, it submits all transactions but it doesn't duplicate existing ones, it will overwrite them.(then they will remain unchanged and unique)

_gaq.push(['_trackTrans']);