I'm trying to set up Google Analytics on a stores 'thank you' page to track purchases but I'm struggling with getting each of the items bought through to Google Analytics.
The current code I have is getting the last products details but I need all of the items sold details.
This is the PHP I'm working with:
<? $i=1;foreach($previousorder->LineItems->OrderDetailLineItems as $order){
$previousOrderHTML = "";
if($i == 2){
$i=1;
$class = " odd";
}else{
$i++;
$class = "";
}
if(!$order->LineDiscountAmountGross == "0"){
$lineDiscount = sprintf('<span class="required">-%s</span>', $locale->formatCurrency($order->LineDiscountAmountGross, $nwc->getTemplateValue("lb_CurrencySymbol")));
}else{
$lineDiscount = "-";
}
$previousOrderHTML .= sprintf('<tr><td align="left" valign="top" class="extra-padding first-column details-row-one%s">%s</td><td align="left" valign="top" class="item-name extra-padding details-row-one%s">%s %s %s', $class, $order->ProductCode, $class, $order->ProductGroupName, $order->Size, $order->Colour);
if(isset($order->BundleProductList->PriceListBundleProductList) && count($order->BundleProductList->PriceListBundleProductList) >= 1){
$previousOrderHTML .= '<span class="offer-small">';
foreach($order->BundleProductList->PriceListBundleProductList as $bundleItem){
$previousOrderHTML .= sprintf("<br />- %s %s %s (%s) - %s %s", $bundleItem->ProductGroupName, $bundleItem->Size, $bundleItem->FabricColour, $bundleItem->ProductCode, $nwc->getValue("lb_RRP"), $locale->FormatCurrency($bundleItem->Original_GROSS_Price));
}
$previousOrderHTML .= '</span>';
}
$previousOrderHTML .= sprintf('</td><td align="left" valign="top" class="extra-padding details-row-one%s">%s</td><td align="center" valign="top" class="extra-padding details-row-one%s">%s</td><td align="right" valign="top" class="extra-padding details-row-one%s">%s</td></td><td align="right" valign="top" class="extra-padding details-row-one last-column%s">%s</td></tr>', $class, $locale->formatCurrency($order->RRP, $nwc->getTemplateValue("lb_CurrencySymbol")), $class, $order->Quantity, $class, $lineDiscount, $class, $locale->formatCurrency($order->GrossDiscountPrice, $nwc->getTemplateValue("lb_CurrencySymbol")));
echo $previousOrderHTML;
}
?>
And this is the Javascript to push the data to the datalayer for Google Tag Manager and Google Analytics
<script type="text/javascript">
$(function(){
var cartTot = '<?=$previousorder->TotalOrderAmountExVAT;?>';
var curr = '<?=$ShoppingCartSummary->Currency;?>';
var ref = '<?=$previousorder->OurReferenceNumber;?>';
var vatx = '<?=$previousorder->TotalVATAmount;?>';
var prodid = '<?=$order->ProductCode;?>';
var prodname = '<?=$order->ProductGroupName;?>';
var rrp = '<?=$order->RRP;?>';
var qnty = '<?=$order->Quantity;?>'
dataLayer.push({
'ecommerce': {
'purchase': {
'actionField': {
'id': ref, // Transaction ID. Required for purchases and refunds.
'affiliation': 'Online Store',
'revenue': cartTot, // Total transaction value (incl. tax and shipping)
'tax': vatx,
'shipping': '',
},
'products': [{ // List of productFieldObjects.
'name': prodname, // Name or ID is required.
'id': prodid,
'price': rrp,
'quantity':qnty, // Optional fields may be omitted or set to empty string.
}]
}
}
});
What can I do to get the product details of each item sold, please?
Many thanks