Post by protechtrader » Tue Jun 01, 2021 12:23 am

Hello, I am trying to use the gtag.js conversion tracking code in opencart 1.5.6 and I have been able to get it to work sending the order value variables from the checkout controller to the success.tpl, but I can not seem to get them in the header.tpl from the header controller.

In the header.tpl i used a php if to show code on the checkout/success page sending the route and variables from the header controller, but it does not seem to be working. Can anyone assist with the code I would need to send the order value & id to the header.tpl along with the php if route statement to only show it on the success page (but in the header) ?

Thank you!

New member

Posts

Joined
Fri Nov 01, 2013 12:20 am

Post by straightlight » Tue Jun 01, 2021 1:00 am

Marketplace request.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by protechtrader » Tue Jun 01, 2021 9:00 am

straightlight wrote:
Tue Jun 01, 2021 1:00 am
Marketplace request.
Unhelpful reply.

New member

Posts

Joined
Fri Nov 01, 2013 12:20 am

Post by protechtrader » Tue Jun 01, 2021 9:02 am

This is the code I am trying to use in header.php:

Code: Select all

if (!empty($this->request->get['route'])) {
    $data['route'] = $this->request->get['route'];
} else {
    $data['route'] = '';
}
This is the code from header.tpl:

Code: Select all

<!-- Global site tag (gtag.js) - Google Ads: 1006202476 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-36630190-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-36630190-1');
gtag('config', 'AW-1006202476');
</script>

<?php if (isset($route) && $route == 'checkout/success') { ?>

<!-- Event snippet for Purchase conversion page -->
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-1006202476/pAthCMzxuQgQ7Nzl3wM',
      'value': <?php echo $total; ?>,
      'currency': 'USD',
      'transaction_id': <?php echo $order_id; ?>
  });
</script>

Can anyone see why this may not be working?

New member

Posts

Joined
Fri Nov 01, 2013 12:20 am

Post by halfhope » Tue Jun 01, 2021 3:01 pm

Are you having a closing tag for if?
Use <?php echo var_dump($route); ?> to trace var value. Also you can remove isset because $route is set anyway

My extensions in marketplace. [ security | flexibility | speedup ]


User avatar
Active Member

Posts

Joined
Tue Dec 10, 2013 9:44 pm
Location - San Diego

Post by ADD Creative » Tue Jun 01, 2021 5:09 pm

Code: Select all

<?php if (isset($route) && $route == 'checkout/success') { ?>
You would be better doing that bit in the controller. Then check there is an order ID.

Code: Select all

if (isset($this->request->get['route'])) && $this->request->get['route']) == 'checkout/success') { 
	if (isset($this->session->data['order_id'])) {
		$this->load->model('checkout/order');

		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

		if ($order_info) {
			$this->data['ecommerce_analytics'] =
Note for 1.5.x it's $this->data['value'] not $data['value'].

Then in the template.

Code: Select all

<?php if (!empty($ecommerce_analytics)) {
	// Your analytics script
} ?>

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom
Who is online

Users browsing this forum: No registered users and 123 guests