Forum

Thread tagged as: Question, Shop

Google Ecommerce Tracking

Hi,

I am looking at implementing Google e-commerce tracking in the new Shop. I have found a good example here:

Ecommerce Example

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.

Mike Harrison

Mike Harrison 37 points

  • 5 years ago

The start of my $order array looks like this, so I am not sure why I am getting the undefined offset message

Array
(
    [0] => Array
        (
            [_status] => paid
            [orderID] => 799
            [orderStatus] => paid
            [orderInvoiceNumber] => #445
            [orderGateway] => stripe
            [orderTotal] => 52.95
            [orderItemsSubtotal] => 39.17
            [orderItemsTotal] => 47
            [orderShippingSubtotal] => 4.96
            [orderShippingDiscounts] => 0.00
            [orderItemsTax] => 7.83
            [orderTaxTotal] => 8.83
            [orderShippingTotal] => 5.95
            [orderDiscountsTotal] => 0.00
Drew McLellan

Drew McLellan 2638 points
Perch Support

You have $order['0'] but there's no item with a string key of 0, so try item index zero: $order[0]

HI Drew, I have made that change still not having any luck :(

I now have this:

function getTransactionJs(&$order) {
  return <<<HTML
ga('ecommerce:addTransaction', {
  'id': '{$order[0]['orderID']}',
  'revenue': '{$order[0]['grand_total']}',
  'shipping': '{$order[0]['shipping_with_tax']}'
});
HTML;
}

Which is outputting:

ga('ecommerce:addTransaction', {
  'id': '',
  'revenue': '',
  'shipping': ''
});

Oddly, if I do the following:

$order_id = $order[0]['orderID'];
echo $order_id;

It echoes correctly, though I am still getting the Undefined Offset PHP error for it

Drew McLellan

Drew McLellan 2638 points
Perch Support

When you print_r($order) are you doing that inside getTransactionJs() ?

No I am not. This is what the top of my page looks like currently:

    $order_id = perch_shop_successful_order_id();
    $order = perch_shop_order($order_id, [
      'skip-template' => true
    ], true);

    // 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;
}

// 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;
}

PerchUtil::debug($order);
Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

<script>
ga('require', 'ecommerce');

<?php
echo getTransactionJs($order);
?>

ga('ecommerce:send');
</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