Google Analytics Plugin - trigger on the last page

  • Posts: 53
  • Thank you received: 2
8 years 9 months ago #230207

-- url of the page with the problem -- : www.vitaking.ro/
-- HikaShop version -- : 2.6.1
-- Joomla version -- : 3.4.8
-- PHP version -- : 5.5.9-1ubuntu4.14
-- Browser(s) name and version -- : Firefox 44

Hello,

We need a litle help about triggering the Google Analytics Plugin on the last page (thank you page) on the checkout process. Right now the plugin is triggered in the admin page on changing the order status to Confirmed. The problem with his is that we can't track the source of the customers in Analytics, so we want to move this on the last checkout page. In this way we can have the correct source of the customers.

I think the easiest way would be to move some code from the plugin to the /template/html/com_hikashop/checkout/end.php. We have the code as follows:

$js = '
<!-- Google Analytics ecommerce -->
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");

ga("create", "' . $account . '", "auto");
ga("send", "pageview");'.$extra_required.'
ga("require", "ecommerce", "ecommerce.js");

ga("ecommerce:addTransaction", {
	"id": "' . $order->order_id.'",
	"affiliation": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), $siteName) . '",
	"revenue": "' . round($order->order_full_price, 2) . '",
	"shipping": "' . round($order->order_shipping_price, 2) . '",
	"tax": "' . round($tax, 2) . '"
});
';

			foreach($order->products as $product) {
				$js .= '
ga("ecommerce:addItem", {
	"id": "' . $order->order_id . '",
	"name": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), strip_tags($product->order_product_name)) . '",
	"sku": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), $product->order_product_code) . '",
	"category": "",
	"price": "' . ($product->order_product_price + $product->order_product_tax) . '",
	"quantity": "' . (int)$product->order_product_quantity . '"
});
';
			}

			$js .= '
ga("ecommerce:send");
</script>
<!-- End Google Analytics -->
';

echo $js;

Of course this doesn't produce any valuable output, as the classes and variables aren't initialized ($order, $product, and so on).

Can you please give us some help in this? Thanks!

Regards,
Dezso

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
8 years 9 months ago #230251

Hi,

Here is a thread with some cod you can use to get the data of the order:
www.hikashop.com/forum/4-how-to/68733-ge...id-in-after-end.html

The following user(s) said Thank You: otx

Please Log in or Create an account to join the conversation.

  • Posts: 53
  • Thank you received: 2
8 years 9 months ago #230603

Hi Nicolas,

Thanks for your help, I'm almost done. I will paste the code here just for documentation purposes. This is a working solution for anyone who wants the ecommerce data on the last checkout page.

$siteName = 'your site name';

$account= 'your analytics account (UA-)';

$app = JFactory::getApplication();
$order_id = $app->getUserState('com_hikashop.order_id');
$orderClass = hikashop_get('class.order');
$this->order = $orderClass->loadFullOrder($order_id); 

$order_total = round($this->order->order_full_price, 2);
$shipping_price =  round($this->order->order_shipping_price, 2);
$revenue = $order_total - $shipping_price;
$order_total_no_vat = round($this->order->order_subtotal_no_vat, 2);
$tax =  $order_total - $order_total_no_vat;

$js = '
<!-- Google Analytics ecommerce -->
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");

ga("create", "' . $account . '", "auto");
ga("send", "pageview");'.$extra_required.'
ga("require", "ecommerce", "ecommerce.js");

ga("ecommerce:addTransaction", {
	"id": "' . $order_id .'",
	"affiliation": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), $siteName) . '",
	"revenue": "' . $revenue . '",
	"shipping": "' . $shipping_price . '",
	"tax": "' . round($tax, 2) . '"
});
';

foreach($this->order->products as $product) {
	$js .= 
	'ga("ecommerce:addItem", {
	"id": "' . $order_id . '",
	"name": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), strip_tags($product->order_product_name)) . '",
	"sku": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), $product->order_product_code) . '",
	"category": "",
	"price": "' . ($product->order_product_price + $product->order_product_tax) . '",
	"quantity": "' . (int)$product->order_product_quantity . '"
	});
	';
}

$js .= '
ga("ecommerce:send");
</script>
<!-- End Google Analytics -->
';

echo $js;

Place this code in the end.php in your template html directory.
There is only one problem with this: it doesn't save the category of the products. Even in the original plugin there is only an empty "" for this. Is there a way to have this information saved and used on the checkout process?

Regards,
Dezso

The following user(s) said Thank You: Kumigy

Please Log in or Create an account to join the conversation.

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
8 years 9 months ago #230611

Hi,

You would need to load the categories of each product with a MySQL query based on $product->product_id and on the tables hikashop_product_category and hikashop_category in order to be able to load the category names and then place them there.

Please Log in or Create an account to join the conversation.

Time to create page: 0.061 seconds
Powered by Kunena Forum