Making ISC Ecommerce tracking code work with Google Analytics gtag.js

Problem

The ISC module is using the old traditional tracking code, instead of the new gtag.js code.

Prerequisite

  • You are using Global Site Tag (gtag.js) tracking code
  • Ecommerce and Enhanced Ecommerce Reporting are enabled in Google Analytics

Solution

INCOMPLETE: Only “purchase” and “view_item ” events have been implemented. To get full reports in Google Analytics, other events should be also be logged, e.g. add_to_cart, add_to_wishlist, begin_checkout, search, etc

At /modules/analytics/googleanalytics/module.googleanalytics.php (line 133)

Find

    $conversionCode = "
     <script type=\"text/javascript\">
     $(document).ready(function() {
      if(typeof(pageTracker) != 'undefined') {
       pageTracker._addTrans(
        '".$order['orderid']."',
        '".addslashes(GetConfig('StoreName'))."',
        '".number_format($this->GetTotalAmount(), 2, '.', '')."',
        '".number_format($this->GetTaxCost(), 2, '.', '')."',
        '".number_format($this->GetShippingCost(), 2, '.', '')."',
        '".$billingAddress['ordbillsuburb']."',
        '".$billingAddress['ordbillstate']."',
        '".$billingAddress['ordbillcountry']."'
       );
    ";
    while($product = $GLOBALS['ISC_CLASS_DB']->Fetch($productResult)) { 
     $productId = $product['ordprodid'];
     if($product['ordprodvariationid'] > 0) {
      $productId .= '-'.$product['ordprodvariationid'];
     }
     $prodCode = $product['ordprodsku'];
     if (empty($prodCode)) {
      $prodCode = $product['ordprodid'];
     }

     $conversionCode .= "
      pageTracker._addItem(
       '".$order['orderid']."',
       '".addslashes($prodCode)."',
       '".addslashes($product['ordprodname'])."',
       '',
       '".number_format($product['price_inc_tax'], 2, '.', '')."',
       '".$product['ordprodqty']."'
      );
     ";
    }
    $conversionCode .= "
      pageTracker._trackTrans();
     }
    });
    </script>";

Replace

    $conversionCode = "
        <script type=\"text/javascript\">
         gtag('event', 'purchase', {
       \"transaction_id\": \"".$order['orderid']."\",
       \"value\": ".number_format($this->GetTotalAmount(), 2, '.', '').",
       \"currency\": \"USD\",
       \"tax\": ".number_format($this->GetTaxCost(), 2, '.', '').",
       \"shipping\": ".number_format($this->GetShippingCost(), 2, '.', '').",
       \"items\": [

    ";
    while($product = $GLOBALS['ISC_CLASS_DB']->Fetch($productResult)) { 
        $productId = $product['ordprodid'];
        if($product['ordprodvariationid'] > 0) {
            $productId .= '-'.$product['ordprodvariationid'];
        }
        $prodCode = $product['ordprodsku'];
        if (empty($prodCode)) {
            $prodCode = $product['ordprodid'];
        }

        $conversionCode .= "
         {
          \"id\": \"".addslashes($prodCode)."\",
          \"name\": \"".addslashes($product['ordprodname'])."\",
          \"quantity\": ".$product['ordprodqty'].",
          \"price\": ".number_format($product['price_inc_tax'], 2, '.', '')."
         },
        ";
    }
    $conversionCode .= "
              ]
     });
    </script>";

At /templates/<TemplateName>/Panel/ProductDetails.html (end of file)

Add

<script>
gtag('event', 'view_item', {
  "items": [
    {
      "id": "%%GLOBAL_SKU%%",
      "name": "%%GLOBAL_ProductName%%",
      "brand": "%%GLOBAL_BrandName%%",
      "quantity": 1,
      "price": %%GLOBAL_SchemaPrice%%
    }
  ]
});
</script>

Testing

You should verify if events are being sent correctly using Google Tag Assistant and check Ecommerce reports in Google Analytics

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *