Page 1 of 1

Tracking coupon code from Analytics

PostPosted: Mon Apr 16, 2018 3:32 pm
by mile2
Hi Stan,
Is OPC sending Tracking coupon code to Analytics? We are using AwoCoupon and when i go to analytics i don't see my coupon codes?

Thanks

Re: Tracking coupon code from Analytics

PostPosted: Tue Apr 17, 2018 11:51 am
by admin
hello, the coupon code is implemented within the very latest OPC's GA tracking via GTAG

as
Code: Select all
<script>
    gtag('event', 'purchase', {
     'transaction_id': '<?php echo $this->escapeSingle($idformat); ?>',
    'affiliation':  '<?php echo $this->escapeSingle($this->vendor['company']); ?>',<?php
          $app = JFactory::getApplication();
         if ($app->getName() === 'administrator') {
            ?>
   'non_interaction': true, <?php
         } ?>
    'value': <?php echo number_format($order_total, 2, '.', ''); ?>,             
    'currency': '<?php echo $this->escapeSingle($this->currency['currency_code_3']); ?>',   
    'tax': <?php echo number_format($this->order['details']['BT']->order_tax, 2, '.', ''); ?>,
     'shipping': <?php echo number_format($this->order['details']['BT']->order_shipment, 2, '.', ''); ?>,
    'coupon': '<?php if (!empty($this->order['details']['BT']->coupon_code)) echo $this->escapeSingle($this->order['details']['BT']->coupon_code); ?>',
     'items': <?php 
     $max = count($this->order['items']);
     $i = 0;
     $items = array();
     foreach ($this->order['items'] as $key=>$order_item)
     {
    
     $product_id = $order_item->pid;
       $i++;
     if (empty($order_item->category_name)) $order_item->category_name = '';
     if (!empty($order_item->virtuemart_category_name)) $order_item->category_name = $order_item->virtuemart_category_name;
    
     $json = array();
       $json['id'] = (string)$product_id;
       $json['name'] = $order_item->order_item_name;
        $json['list_name'] = 'Purchase';
      $json['brand'] = '';
      $json['category'] = $order_item->category_name;
        $json['variant'] = $order_item->order_item_sku;
      $json['list_position'] = $i;
      $json['price'] = (float)number_format($order_item->product_final_price, 2, '.', '');
        $json['quantity'] = (int)number_format($order_item->product_quantity , 0, '.', '');
      if (!empty($this->order['details']['BT']->coupon_code))
        $json['coupon'] = $this->order['details']['BT']->coupon_code;
       $items[] = $json;
    
     }
     echo json_encode($items, JSON_PRETTY_PRINT);
      ?> });
   
   
<?php

    $root = Juri::root();
    if (substr($root, -1) !== '/') $root .= '/';
    if (empty($this->params->page_url)) $this->params->page_url = '/thank-you';
    if (substr($this->params->page_url, 0, 1) === '/') $cart_path = substr($this->params->page_url, 1);
    else {
       $cart_path = $this->params->page_url;
       $this->params->page_url = '/'.$this->params->page_url;
    }
    if (empty($this->params->page_title)) $this->params->page_title = 'OPC Checkout';
    if (empty($GLOBALS['_gtag'])) {
    $GLOBALS['_gtag'] = new stdClass();
    $GLOBALS['_gtag']->page_title = $this->params->page_title;
    $GLOBALS['_gtag']->page_location = $root.$cart_path;
    $GLOBALS['_gtag']->page_path = $this->params->page_url;
   
    }


if (!empty($this->params->adwrods_conversion_id)) { ?>

  gtag('event', 'conversion', {
      'send_to': '<?php echo $this->params->google_analytics_id; ?>/<?php echo $this->params->adwrods_conversion_id; ?>',
      'value': <?php echo number_format($order_total, 2, '.', ''); ?>,
      'currency': '<?php echo $this->escapeSingle($this->currency['currency_code_3']); ?>',   
      'transaction_id': '<?php echo $this->escapeSingle($idformat); ?>',
     <?php
         $app = JFactory::getApplication();
         if ($app->getName() === 'administrator') {
            ?> 'non_interaction': true <?php
         }
         ?>
  });

<?php } ?>
   
    if ((typeof console != 'undefined')  && (typeof console.log != 'undefined')  &&  (console.log != null))
     {
        console.log('OPC Tracking gtag: Purchase.');
     }
   
    </script>



we'll need more feedback on the new GTAG implementation.

coupon code is also included in our GTM implementation:

Code: Select all
dataLayer.push({
  'ecommerce': {
    'purchase': {
      'actionField': {
        'id': '<?php echo $this->escapeSingle($idformat); ?>',                         // Transaction ID. Required for purchases and refunds.
        'affiliation':  '<?php echo $this->escapeSingle($this->vendor['company']); ?>',
        'revenue': <?php echo number_format($order_total, 2, '.', ''); ?>,                     // Total transaction value (incl. tax and shipping)
        'tax': <?php echo number_format($this->order['details']['BT']->order_tax, 2, '.', ''); ?>,
        'shipping': <?php echo number_format($this->order['details']['BT']->order_shipment, 2, '.', ''); ?>,
        'coupon': '<?php if (!empty($this->order['details']['BT']->coupon_code)) echo $this->escapeSingle($this->order['details']['BT']->coupon_code); ?>'
      },
      'products': [<?php
    
     $max = count($this->order['items']);
     $i = 0;
     foreach ($this->order['items'] as $key=>$order_item)
     {
    
     $product_id = $order_item->pid;
    
    
     $i++;
    
     if (empty($order_item->category_name)) $order_item->category_name = '';
     if (!empty($order_item->virtuemart_category_name)) $order_item->category_name = $order_item->virtuemart_category_name;
    
     ?>{                           
        'name': '<?php echo $this->escapeSingle($order_item->order_item_name); ?>',     
        'id': '<?php echo $this->escapeSingle($product_id); ?>',
        'price': <?php echo number_format($order_item->product_final_price, 2, '.', ''); ?>,
        'brand': '',
        'category': '<?php echo $this->escapeSingle($order_item->category_name ); ?>',
        'variant': '<?php echo $this->escapeSingle($order_item->order_item_sku); ?>',
        'quantity': <?php echo number_format($order_item->product_quantity , 0, '.', ''); ?>,
        'coupon':  '<?php if (!empty($this->order['details']['BT']->coupon_code)) echo $this->escapeSingle($this->order['details']['BT']->coupon_code); ?>'
       }<?php if ($i !== $max) echo ','; ?>
   <?php } ?>]
    }
  }
});


it's up to awo to store the coupon code properly within the order detials. if it uses html within the coupon code, GTAG or GTM can refuse it.

best regards, stan

Re: Tracking coupon code from Analytics

PostPosted: Wed Apr 18, 2018 3:13 am
by mile2

Re: Tracking coupon code from Analytics

PostPosted: Thu Apr 19, 2018 2:11 am
by mile2
From AwoCoupon developer:

I think you should get back to OPC, as coupon codes in analytics is not within the scope of AwoCoupon. I have verified that the coupon codes stored in the db in Virtuemart are not in html, but plain text. If an order users more than 1 coupon code, then they are both entered in the coupon_code field separated by a comma. Other than that, it is pretty straight forward.

Re: Tracking coupon code from Analytics

PostPosted: Thu Apr 19, 2018 7:07 pm
by mile2
So what you think about his reply?

Re: Tracking coupon code from Analytics

PostPosted: Mon Apr 23, 2018 12:38 pm
by admin
hello, okay, but what is the problem ? opc sends "cart->couponCode" or "orderdetails->coupon_code" to GA, or do you have any other experience ? are you using OPC's GA tracking of the latest version ? are you using GTAG, analytics.js or ga or basic or advanced ecommerce ? are you using GTM separately, or togher with GA ? are you using adwords with GTM, etc... ?

each of these got a bit different set up, but withint the very latest OPC there is GTAG support for both Adwords + GA which uses datalayer objects that are sent to GA and part of them is the coupon code.

best regards,stan

Re: Tracking coupon code from Analytics

PostPosted: Mon Apr 23, 2018 3:36 pm
by mile2
GA Screenshots: https://prnt.sc/j6npzd
https://prnt.sc/j6nq3i

Where it says: 0 this is not the real coupon code...

My Settings:

https://prnt.sc/j9048g

Re: Tracking coupon code from Analytics

PostPosted: Thu May 03, 2018 12:53 pm
by admin
hello, within the today's release we are releasing new GTAG updated code, if you'd still have problems with the coupon, let us know.

to see if the coupon code is properly sent to google you may need to use the chrome dev tools to see the rendered coded at the thank you page.

best regards, stan