0

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

Aaron
  • 63
  • 4
  • 14

1 Answers1

0

In the loop: foreach($order->BundleProductList->PriceListBundleProductList as $bundleItem){ '... } Try to create with PHP an array of objects (list of productFieldObjects) in a JavaScript variable like this '[{...},{...},{...}]', example:

[
  {'name': prodname1,
   'id': prodid1,
   'price': rrp1,
   'quantity':qnty1
   },
  {'name': prodname2,
   'id': prodid2,
   'price': rrp2,
   'quantity':qnty2
   },
  {'name': prodname3,
   'id': prodid3,
   'price': rrp3,
   'quantity':qnty3
   }
]

Then in the push, assign this value to the 'product' key:

//...
'purchase': {
    'actionField': {
    'id': '...',
    'affiliation': '...',
    'revenue': ...,
    'tax': ...,
    'shipping': ...,
},
'products': YOUR_ARRAY_OF OBJECTS // <-- HERE
//...
Michele Pisani
  • 13,567
  • 3
  • 25
  • 42
  • Thank you Michele - I tried this but it didn't work. None of the data gets pushed to the data layer – Aaron Jan 02 '20 at 10:03
  • Have you configured the 'purchase' event tag in the Tag Manager (fired by a custom event with name 'purchase') and activate 'Enable Enhanced Ecommerce Features' and 'Use data layer' in that tag? However, if the problem is not sending to analytics but only the push values ​​you can solve as described above, it depends on how you build the variable in the loop, check that you are not getting JavaScript errors in the console. – Michele Pisani Jan 02 '20 at 10:49