0

I am trying to install Google Analytics e-Commerce Tracking on a website, and while I have it installed in the right place, I am unsure of how to use my PHP code to dynamically create e-Commerce Transactions. The example js code that Google provides looks like this:

<script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
 _gaq.push(['_trackPageview']);
 _gaq.push(['_addTrans',
 '1234', // transaction ID - required
 'Acme Clothing', // affiliation or store name
 '11.99', // total - required
 '1.29', // tax
 '5', // shipping
 'San Jose', // city
 'California', // state or province
 'USA' // country
 ]);
 // add item might be called for every item in the shopping cart
 // where your ecommerce engine loops through each item in the cart and
 // prints out _addItem for each
 _gaq.push(['_addItem',
 '1234', // transaction ID - required
 'DD44', // SKU/code - required
 'T-Shirt', // product name
 'Green Medium', // category or variation
 '11.99', // unit price - required
 '1' // quantity - required
 ]);
 _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
 (function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
</script>

Let's say that my transaction ID is a PHP variable called $ID, total is a variable called $amount, etc.

How do I use these variables in my tracking code to dynamically send the correct information to Google?

asked Apr 15, 2015 at 20:26

2 Answers 2

1

If the Javascript is in your HTML output, you can just put the PHP echo right inline (assuming you are using .php files and not .html files:

<script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
 _gaq.push(['_trackPageview']);
 _gaq.push(['_addTrans',
 <?php echo $ID; ?>, // transaction ID - required
 'Acme Clothing', // affiliation or store name
 '11.99', // total - required
 '1.29', // tax
 '5', // shipping
 'San Jose', // city
 'California', // state or province
 'USA' // country
 ]);
 // add item might be called for every item in the shopping cart
 // where your ecommerce engine loops through each item in the cart and
 // prints out _addItem for each
 _gaq.push(['_addItem',
 <?php echo $ID; ?>, // transaction ID - required
 'DD44', // SKU/code - required
 'T-Shirt', // product name
 'Green Medium', // category or variation
 '11.99', // unit price - required
 '1' // quantity - required
 ]);
 _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
 (function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
</script>

Or if your javascript is in a script file, you could write the javascript variable to the html page prior to loading the javascript file then use this new variable (in the example below transactionID):

<script>
 var transactionID = <?php echo $ID; ?>;
</script>
<script src="js/myscript.js"></script>
answered Apr 15, 2015 at 20:38
Sign up to request clarification or add additional context in comments.

Comments

1
<?php
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId()); 
$subtotal = $order->getSubtotal();
$order_id = $order->getId(); //the id of the order
//$order->getIncrementId();//the increment id of the order
$gtotal = $order->getGrandTotal();//grand total of the order 
$address = $order->getBillingAddress()->getData();
$city = $address['city'];
$state = $address['region'];
$country = $address['country_id'];
$shippingVal=$order->getShippingAmount();
$taxAmount=$order->getTaxAmount();
?>
<script type="text/javascript">
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
 'id': '<?php echo $order_id; ?>', // Transaction ID. Required.
 'affiliation': '', // Affiliation or store name.
 'revenue': '<?php echo $gtotal;?>', // Grand Total.
 'shipping': '<?php echo $shippingVal;?>', // Shipping.
 'tax': '<?php echo $taxAmount;?>' // Tax.
});
<?php 
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$product = Mage::getModel('catalog/product')->load($_item['product_id']);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
 $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
 $category = $_cat->getName(); 
 }
 ?>
ga('ecommerce:addItem', {
 'id': '<?php echo $order_id; ?>', // Transaction ID. Required.
 'name': '<?php echo $item->getName(); ?>', // Product name. Required.
 'sku': '<?php echo $item->getSku(); ?>', // SKU/code.
 'category': '<?php echo $category; ?>', // Category or variation.
 'price': '<?php echo $item->getPrice(); ?>', // Unit price.
 'quantity': '<?php echo $item->getQtyToInvoice(); ?>' // Quantity.
});
<?php 
}
?> 
ga('ecommerce:send');
</script>

Thought am late to the party but this should do on the success.phtml file for the active template. Note that u must Have Universal Analytics to make things work in this syntax.

answered Jun 25, 2015 at 14:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.