Forum
Google Ecommerce Tracking
Hi,
I am looking at implementing Google e-commerce tracking in the new Shop. I have found a good example here:
Which looks like what I am after. I am outputting the order as an array on my success page like this:
$order_id = perch_shop_successful_order_id();
$order = perch_shop_order($order_id, [
'skip-template' => true
], true);
Which I think contains all the information I need. I am just not sure quite how I need to adjust the code in the example to use the data from Shop. This is the example from Google:
Their example transaction data:
<?php
// Transaction Data
$trans = array('id'=>'1234', 'affiliation'=>'Acme Clothing',
'revenue'=>'11.99', 'shipping'=>'5', 'tax'=>'1.29');
// List of Items Purchased.
$items = array(
array('sku'=>'SDFSDF', 'name'=>'Shoes', 'category'=>'Footwear', 'price'=>'100', 'quantity'=>'1'),
array('sku'=>'123DSW', 'name'=>'Sandles', 'category'=>'Footwear', 'price'=>'87', 'quantity'=>'1'),
array('sku'=>'UHDF93', 'name'=>'Socks', 'category'=>'Footwear', 'price'=>'5.99', 'quantity'=>'2')
);
?>
Their logic to convert to Javascript string:
<?php
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$trans) {
return <<<HTML
ga('ecommerce:addTransaction', {
'id': '{$trans['id']}',
'affiliation': '{$trans['affiliation']}',
'revenue': '{$trans['revenue']}',
'shipping': '{$trans['shipping']}',
'tax': '{$trans['tax']}'
});
HTML;
}
// Function to return the JavaScript representation of an ItemData object.
function getItemJs(&$transId, &$item) {
return <<<HTML
ga('ecommerce:addItem', {
'id': '$transId',
'name': '{$item['name']}',
'sku': '{$item['sku']}',
'category': '{$item['category']}',
'price': '{$item['price']}',
'quantity': '{$item['quantity']}'
});
HTML;
}
?>
Which then is used in a script:
<!-- Begin HTML -->
<script>
ga('require', 'ecommerce');
<?php
echo getTransactionJs($trans);
foreach ($items as &$item) {
echo getItemJs($trans['id'], $item);
}
?>
ga('ecommerce:send');
</script>
I have the following for the overall transaction information, which I thought was correct but isn't outputting anything:
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$order) {
return <<<HTML
ga('ecommerce:addTransaction', {
'id': '{$order['0']['orderID']}',
'revenue': '{$order['0']['grand_total']}',
'shipping': '{$order['0']['shipping_with_tax']}'
});
HTML;
}
Outputs:
<script>
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': '',
'revenue': '',
'shipping': ''
});
ga('ecommerce:send');
</script>
And I am getting an Undefined offset: 0
message in PHP error log
Also I am not sure how to handle the individual item data. Help greatly appreciated! Will be blogging this when I get it sorted as I think a few people will want to be doing it.
The start of my
$order
array looks like this, so I am not sure why I am getting the undefined offset messageYou have
$order['0']
but there's no item with a string key of0
, so try item index zero:$order[0]
HI Drew, I have made that change still not having any luck :(
I now have this:
Which is outputting:
Oddly, if I do the following:
It echoes correctly, though I am still getting the Undefined Offset PHP error for it
When you
print_r($order)
are you doing that insidegetTransactionJs()
?No I am not. This is what the top of my page looks like currently:
Where is
getTransactionJs()
being called? Are you passing in$order
when you call it?I don't think this really looks like a Perch issue.
It is being called at the bottom of the page, in a script:
You are right it is probably not a Perch issue, I will soldier on and see how I get on
Fixed it - error with my code structure. Thanks for taking the time Drew