AdWords
4.6K members online now
4.6K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

how to write google ecomerce tracking code in php OR FOR JOOMLA ?

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

HELLO 

 

i  rad this article  https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce ,  but I am not getting how the final code is generating  ..??,I need to add both this php and js code on thankyou page 

 

 

<?php
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$trans) {
 
return <<<HTML
ga
('ecommerce:addTransaction', {
 
'id': '{$trans['id']}',
 
'affiliation': '{$trans['affiliation']}',
 
'revenue': '{$trans['revenue']}',
 
'shipping': '{$trans['shipping']}',
 
'tax': '{$trans['tax']}'
});
HTML
;
}

// Function to return the JavaScript representation of an ItemData object.
function getItemJs(&$transId, &$item) {
 
return <<<HTML
ga
('ecommerce:addItem', {
 
'id': '$transId',
 
'name': '{$item['name']}',
 
'sku': '{$item['sku']}',
 
'category': '{$item['category']}',
 
'price': '{$item['price']}',
 
'quantity': '{$item['quantity']}'
});
HTML
;
}
?>

 

 

<!-- Begin HTML -->
<script>
ga
('require', 'ecommerce');

<?php
echo getTransactionJs
($trans);

foreach
($items as &$item) {
  echo getItemJs
($trans['id'], $item);
}
?>

ga
('ecommerce:send');
</script>
2 Expert replyverified_user

Re: how to write google ecomerce tracking code in php OR FOR JOOMLA ?

Community Manager
# 2
Community Manager
Hi Neha,
I would suggest that you post this question to the Google Analytics forum: https://productforums.google.com/forum/#!forum/analytics

Kathleen

Re: how to write google ecomerce tracking code in php OR FOR JOOMLA ?

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
ok kathleen I ll ,thanks

Re: how to write google ecomerce tracking code in php OR FOR JOOMLA ?

Top Contributor
# 4
Top Contributor
Hi neha s,

All of this needs to be inserted to the "thank you" page. The PHP functions create the Javascript that record the transaction.

Best of Luck!

Pete
petebardo -- Deadhead doing AdWords

Re: how to write google ecomerce tracking code in php OR FOR JOOMLA ?

Explorer ✭ ✭ ☆
# 5
Explorer ✭ ✭ ☆

Hi Neha,

I am not sure whether you are familiar with javascript and php to modify the code to track e-commerce conversions.

Basically to implement the code you mentioned you need to know the flow how its executed. Because code you mentioned uses functions to pupulate data for ecommerce code.

Actually below page explains it quite well.
https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce

But just to reinterate flow in simple terms

1. Write php code to build transaction and add items objects/arrays like
<?php
// Transaction Data
$trans = array('id'=>'1234', 'affiliation'=>'Acme Clothing',
               'revenue'=>'11.99', 'shipping'=>'5', 'tax'=>'1.29');

// List of Items Purchased.
$items = array(
  array('sku'=>'SDFSDF', 'name'=>'Shoes', 'category'=>'Footwear', 'price'=>'100', 'quantity'=>'1'),
  array('sku'=>'123DSW', 'name'=>'Sandles', 'category'=>'Footwear', 'price'=>'87', 'quantity'=>'1'),
  array('sku'=>'UHDF93', 'name'=>'Socks', 'category'=>'Footwear', 'price'=>'5.99', 'quantity'=>'2')
);
?>


2.  Now write PHP "functions" to render the javascript required to create transaction and items data which is "getTransactionJs" and "getItemJs" in your code

3. Now you have basic blocks ready to execute e-commerce tracking code. add below functionality just below the GA tracking code javascript-

 
ga('require', 'ecommerce');

<?php
echo getTransactionJs($trans);   /* this will render addTransaction fucntionality in JS to browser using function wrote above */

foreach ($items as &$item) {
  echo getItemJs($trans['id'], $item);  /* this will render addItem fucntionality in JS to browser using function wrote above for multiple items*/
}
?>
ga('ecommerce:send');
 

In short flow will be-

    Load the ecommerce plugin  => ga('require', 'ecommerce');
    Adding a Transaction => Call php function "getTransactionJs" to add transaction
    Adding Items => Call php function "getItemJs" to add items in above transaction
    Sending Data =>  ga('ecommerce:send');
    Clearing Data => ga('ecommerce:clear');

Hope this helps.

Cheers
SJ