Mobile app version of vmapp.org
Login or Join
Courtney195

: What are the possible causes of discrepancies in Google Analytics ecommerce tracking? We have setup Google Analytics Ecommerce Tracking through Google Tag Manager. Code: $tracking_script = " dataLayer.push({

@Courtney195

Posted in: #Ecommerce #GoogleAnalytics #Tracking

We have setup Google Analytics Ecommerce Tracking through Google Tag Manager.

Code:

$tracking_script = "
dataLayer.push({
'transactionId': '$ref',
'transactionAffiliation': '$cartec_name',
'transactionTotal': '$amount',
'transactionTax': '',
'transactionShipping': '',
'transactionProducts': [{
'sku': 'DD-{$amount}-{$currency}',
'name': '{$cartec_name}_Deposit',
'category': '',
'price': '$amount',
'quantity': 1
}],
'event': 'deposit_successful'
});
";
<script type="text/javascript">
if (typeof(dataLayer) !== 'undefined') {
<?php echo $tracking_script; ?>
}
</script>


Tracking works but we have about 200 (30%) transactions missing per day compared to our database records.

What are the possible causes and what are the best methods for debugging this problem?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Courtney195

1 Comments

Sorted by latest first Latest Oldest Best

 

@Berryessa370

Your script is running before the GTM's dataLayer object is ready as it's retrieved asynchronously. This means you need to wait the load event is fired/triggered.

Your server side php code is not good as is writing into the dataLayer and definitely is the root issue this code is not awaiting for the GTM's datalayer object. Instead of this just create a variable to be ready for js.

$tracking_script = "var myObject = {your object code}";


Before the push code you need to put somewhere the php code to create the var

<script type="text/javascript">
<?php echo $tracking_script; ?>
</script>


Then you can use the load event to properly inject the server side code into the GTM's dataLayer. You can use the next gist:
gist.github.com/web20opensource/6c67368a2fdb0e712550#file-load-event-attachevent-addeventlistener-cross-browser
Then just add your lines inside the callback function:

var callBack = function(){
var myObject = myObject || {};
dataLayer = dataLayer || [];
dataLayer.push(myObject);
};


Or use jQuery like

$( window ).load(function() {
// Run code
var myObject = myObject || {};
dataLayer = dataLayer || [];
dataLayer.push(myObject);
});

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme