Your account is limited to posting only in the Pre-Sales Questions forum.
Support forum access is limited to members with a purchase history.

If you are working on behalf of another member, please contact us with the name of the license holder and we will upgrade your account.
   
 
How to send purchase info to Google Analytics
Posted: 19 April 2010 09:55 AM   [ Ignore ]
Administrator
Avatar
RankRank
Total Posts:  152
Joined  2010-01-19

If you use Google Analytics on your site then you can track purchases and monitor where customers come from - tons of useful info actually.

To do this you need to add some extra javascript to your “order success” page.

I have a single order_info template that contains a conditional that will show different messages depending on whether the order was successful or not.

To track purchase info, in the “approved” part of this conditional I added the following within the {exp:weblog:entries} tag pair that is outputting the order information.
(be sure to add your Analytics Account ID - it’s normally something like: UA-123456789-1

script opening tag here the forum won't let me include it


                          var _gaq = _gaq || [];
                          _gaq.push(['
_setAccount', 'UA-XXXXXXXX-X']);
                          _gaq.push(['
_trackPageview']);
                          _gaq.push(['
_addTrans',
                            '
{title}',                                                // order ID - required
                            '
Amano Direct Web Shop',                      // affiliation or store name
                            '
{order_total}',                                     // total - required
                            '
{order_tax}',                                       // tax
                            '
{order_shipping}',                               // shipping
                            '',                                                        // city
                            '',                                                        // state or province
                            '
UK'                                                     // 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 
                        {order_items}
                          _gaq.push(['
_addItem',
                            '
{title}',                                               // order ID - required
                            '
{item:entry_id}',                                 // SKU/code
                            '
{item:title}',                                       // product name
                            '
{item:item_option}',                           // category or variation
                            '
{item:price}',                                     // unit price - required
                            '
{item:quantity}'                                 // quantity - required
                          ]);
                        
                        {/order_items}
                        
                          _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';
                            
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
                          
})();



script closing tag here the forum wont let me include it 


My products don’t have unique product codes/SKU so I am using the entry_id but you can use whatever you wish
I’m also not using product options so at this stage I can’t confirm whether that is going to work as is, but the principle is the same.

[ Edited: 07 May 2010 06:12 AM by Pete Eveleigh ]
 Signature 

Pete Eveleigh | Fantastic Machine | Gloucester, UK | vCard | Twitter

Profile
 
 
Posted: 02 August 2011 04:44 AM   [ Ignore ]   [ # 1 ]
Wallflower
Rank
Total Posts:  17
Joined  2011-06-23

Thanks for sharing this.
This is what I used with EE2/CT2:

{exp:channel:entries channel="orders" status="Open" author_id="CURRENT_USER" limit="1"}
{exp
:cartthrob:submitted_order_info}
FORUM_REMOVED_SCRIPT_OPEN _TAG
      
var _gaq _gaq || [];
    
_gaq.push(['_setAccount''UA-XXXXXXX-XX']);
    
_gaq.push(['_trackPageview']);
    
_gaq.push(['_addTrans',
      
'{order_id}',                                                // order ID - required
      
'Your Company Name',                      // affiliation or store name
      
'{total}',                                     // total - required
      
'{tax}',                                       // tax
      
'{shipping}',                               // shipping
      
'',                                                        // city
      
'',                                                        // state or province
      
'US'                                                     // 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
        
{order_items}
    _gaq
.push(['_addItem',
      
'{order_id}',                                               // order ID - required
      
'{item:entry_id}',                                 // SKU/code
      
'{item:title}',                                       // product name
      
'{item:price}',                                     // unit price - required
      
'{item:quantity}'                                 // quantity - required
    
]);
        
{/order_items}

    _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';
      (
document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
    
})();
FORUM_REMOVED_SCRIPT_CLOSE_TAG
{
/exp:cartthrob:submitted_order_info}
{
/exp:channel:entries} 
Profile