Skip to content

Analytics w Ecommerce

Derek Jones edited this page Jul 5, 2012 · 4 revisions

Category:Analytics

I have created a library based on the Analytics helper posted by manilodisan. I made this into a library and added support for ecommerce tracking.

File:Analytics_Lib.zip

Usage: In your controller.php

    $this->load->library('Analytics_Lib');
    
    //Add transaction detail to code
    //add_trans('order_id','Company Name','Order Total','Tax','Shipping','City','State','Country');
    $this->analytics_lib->add_trans('1234','Test Company','200','0','0','City','State','USA');

    //Add order line item detail to code
    //add_item('order_id','SKU','Product Name','Category','Price','Quantity');
    $this->analytics_lib->add_item('1234','16-10','Test Product','Category','100','1');

    //Generate entire Analytics javascript code
    // @trackpage (optional)
    // @UA-Account can be set in config $config['google_uacct']
    //generate('trackpage','UA-Account');
    $data['analytics'] = $this->analytics_lib->generate();

    $this->load->view('page',$data);

In your view ie page.php

    <html>
        <head>
        <title>Site Name</title>
        <?php echo $analytics;?>
        </head>
        <body>
        Body Goes Here
        </body>
    <html>

If you already copied and pasted the Analytics code from your Dashboard, you can add just the ecommerce snippets by the method show below:

In your controller.php

    $this->load->library('Analytics_Lib');
    
    //Add transaction detail to code
    //add_trans('order_id','Company Name','Order Total','Tax','Shipping','City','State','Country');
    $this->analytics_lib->add_trans('1234','Test Company','200','0','0','City','State','USA');

    //Add order line item detail to code
    //add_item('order_id','SKU','Product Name','Category','Price','Quantity');
    $this->analytics_lib->add_item('1234','16-10','Test Product','Category','100','1');
    
    //Generate only pageTracker._addTrans & pageTracker._addItem
    $data['addTrans'] = $this->analytics_lib->getTrans();
    $data['addItem'] = $this->analytics_lib->getItem();

    $this->load->view('page',$data);

In your view page.php

    <html>
        <head>
        <title>Site Name</title>
        < script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
[removed](unescape("[removed][removed]"));
[removed]
< script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._trackPageview();
<?php echo $addTrans;?>
<?php echo $addItem;?>
} catch(err) {}[removed]
        </head>
        <body>
        Body Goes Here
        </body>
    <html>
Clone this wiki locally