+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 20

Thread: Google Analytics E-Commerce Tracking for Squirrelcart 3+

  1. #1
    Registered User ljcaswell's Avatar
    Join Date
    Apr 2010
    Posts
    14
    Squirrelcart version
    v3.4.1

    Google Analytics E-Commerce Tracking for Squirrelcart 3+

    Here's the code I use to track E-Commerce transactions in Google Analytics for SC 3.

    If Analytics is already running on your site, you can ship this part.

    In store_main.tpl.php, within the <head> section, after editing the UA value below, add:

    Code:
    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXX-1']);
      _gaq.push(['_trackPageview']);
    
      (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>
    Then in checkout_done.tpl.php, at the end of the file, edit the UA number and add:

    Code:
    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXX-1']);
      _gaq.push(['_trackPageview']);
      _gaq.push(['_addTrans',
        '<?=$SC['order_placed']['number']?>',           // order ID - required
        'British Tools & Fasteners',  // affiliation or store name
        '<?=$SC['order_placed']['grand_total']?>',          // total - required
        '<?=$SC['order_placed']['tax_total']?>',           // tax
        '<?=$SC['order_placed']['shipping_total']?>',              // shipping
        '<?=htmlentities($SC['order_placed']['address']['Bill_City'])?>',       // city
        '<?=htmlentities($SC['order_placed']['address']['Bill_State_or_Province'])?>',     // state or province
        '<?=htmlentities($SC['order_placed']['address']['Bill_Country'])?>'             // country
      ]);
    
    <?php foreach($SC['order_placed']['items'] as $item) { ?>
              
      _gaq.push(['_addItem',
        '<?=$SC['order_placed']['number']?>',           // order ID - required
        '<?=htmlentities($item['Product_Code'])?>',           // SKU/code - required
        '<?=htmlentities($item['Name'])?>',        // product name
        '<?=$item['primary_category']?>',   // category or variation
        '<?=$item['Base_Price']?>',          // unit price - required
        '<?=$item['qty']?>'               // 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>

  2. #2
    Registered User ljcaswell's Avatar
    Join Date
    Apr 2010
    Posts
    14
    Squirrelcart version
    v3.4.1

    Update

    Actually, it's better to use the updated analytics code in your store_main.tpl.php file, so use this instead:

    Code:
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    var pageTracker = _gat._getTracker("UA-XXXXXX-1");
    pageTracker._trackPageview();
    </script>

  3. #3
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    GA E-Commerce Tracking Code

    Thanks for the code. I'll put this in my site. I have other dilemma, I am trying to setup goals in google analytics to track my orders and I am stuck at "Head Match". I not able to give "Goal URL" as Squirrel Cart has dynamic pages. I tried checkout-done.tpl.php and order-detail.tpl.php. Nothing worked. Do you have any suggestions on how I do I setup goals in Google Analytics and track my orders. Thanks!

  4. #4
    Registered User ljcaswell's Avatar
    Join Date
    Apr 2010
    Posts
    14
    Squirrelcart version
    v3.4.1
    Yep - just posted this as you were replying

    http://www.ldev.com/forums/showthrea...Squirrelcart-3

  5. #5
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    Googlle Analytics Goals

    Thank you for quick reply. I'll try this. I see the you have updated the code for GA tracking in the store-main.tpl.php. I had this earlier at the end of the file. Shall I add this in the head section?

  6. #6
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    GA Code

    Sorry now I see that it is supposed to be the head sections.

  7. #7
    Registered User ljcaswell's Avatar
    Join Date
    Apr 2010
    Posts
    14
    Squirrelcart version
    v3.4.1
    Yes, it needs to go in the head, before any other Analytics javascript, so that the pageTracker object is created first.

  8. #8
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    GA E-Commerce

    Thanks for the code, its' working great but in the Product Revenue it show base price. We have options in our products which adds up to the total price. I would like to have Grand Total in the product revenue. Can I have grand-total field in the script like this instead of base-price :

    <?php foreach($SC['order_placed']['items'] as $item) { ?>

    _gaq.push(['_addItem',
    '<?=$SC['order_placed']['number']?>', // order ID - required
    '<?=htmlentities($item['Product_Code'])?>', // SKU/code - required
    '<?=htmlentities($item['Name'])?>', // product name
    '<?=$item['primary_category']?>', // category or variation
    '<?=$SC['order_placed']['grand_total']?>', // unit price - required
    '<?=$item['qty']?>' // quantity - required
    ]);

    <? } ?>
    or should I have both.Thanks for your help.

  9. #9
    Client coastalrugs's Avatar
    Join Date
    Dec 2007
    Posts
    171
    Squirrelcart version
    v3.2.0
    '<?=$SC['order_placed']['grand_total']?>', // unit price - required
    You may want to use:
    PHP Code:
    print $item['total'
    instead of Grand_Total.

    This pulls the item total, that made of options + base price. Grand_total would be for the whole order, which is fine if all your orders are single items. The earlier portion of the GA code handles adding the Grand_Total value to the analytics data.

    '<?=htmlentities($item['Product_Code'])?>', // SKU/code - required
    An additional note, if your order happens to include 2+ of the same item with different price configurations (resulting in two separate items), Google only remembers the last item's value, which may lead to strange orders that do not seem to add up correctly in the analytics. My solution is to add the item hash to the sku:
    PHP Code:
    print $item['record_number'].'-'.htmlentities($item['SKU'],ENT_QUOTES).'-'.htmlentities($item['Product_Code'],ENT_QUOTES).'-'.$item['hash'
    This makes sure that each item is unique when passed to Google. (It also expands the data to include other data points that may be useful)

  10. #10
    Client coastalrugs's Avatar
    Join Date
    Dec 2007
    Posts
    171
    Squirrelcart version
    v3.2.0
    This is the full altered code for this portion of GA code:

    Code:
    <!-- Google ecommerce tracking -->
    <script type="text/javascript">
        var _gaq = _gaq || [];
        
        _gaq.push(['_setAccount', 'UA-XXXXXX-1']);
        _gaq.push(['_trackPageview']);
        _gaq.push(['_addTrans',
        '<?php print $SC['order_placed']['number'] ?>',        // order ID - required
        'Your Name Here',        // affiliation or store name
        '<?php print $SC['order_placed']['grand_total'] ?>',        // total - required
        '<?php print $SC['order_placed']['tax_total'] ?>',        // tax
        '<?php print $SC['order_placed']['shipping_total'] ?>',        // shipping
        '<?php print htmlentities($SC['order_placed']['address']['Bill_City'],ENT_QUOTES) ?>',        // city
        '<?php print htmlentities($SC['order_placed']['address']['Bill_State_Name'],ENT_QUOTES) ?>',        // state or province
        '<?php print htmlentities($SC['order_placed']['address']['Bill_Country_Name'],ENT_QUOTES) ?>'        // country
        ]);
    <?php foreach($SC['order_placed']['items'] as $item) { ?>
        _gaq.push(['_addItem',
        '<?php print $SC['order_placed']['number'] ?>',        // order ID - required
        '<?php print $item['record_number'].'-'.htmlentities($item['SKU'],ENT_QUOTES).'-'.htmlentities($item['Product_Code'],ENT_QUOTES).'-'.$item['hash'] ?>',        // SKU/code - required
        '<?php print htmlentities($item['Name'],ENT_QUOTES) ?>',        // product name
        '<?php print $item['primary_category'].'-'.htmlspecialchars(get_field_val('Categories','Name','record_number = '.$item['primary_category']),ENT_QUOTES); ?>',        // category or variation
        '<?php print $item['total'] ?>',    // unit price - required
        '<?php print $item['qty'] ?>'        // quantity - required
        ]);
    <?php } ?>
        _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
    </script>
    Notice a couple others alterations:
    - State and Country use a different field, as the original code only added the database record number, not a textual value.
    - Category and Product values are slightly expanded to make them more useful.
    - Some conservative escaping of potential html characters.

  11. #11
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    GA E-Commerce

    Thank you so much. I'll put this code and let you know how it works.

  12. #12
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    GA E-Commerce

    Hi Coastalrugs, I have used your code for tracking but it is not showing anything in the analytics. I have put this code in chekcout-done.tpl.php. Do you have any suggestions please?

  13. #13
    Client coastalrugs's Avatar
    Join Date
    Dec 2007
    Posts
    171
    Squirrelcart version
    v3.2.0
    You will still need the the original code that applies the google-analytics.com/ga.js script file to the page by either method that 'ljcaswell' mentions above.

    The code I attached is just to extract/format the info from the SC order into GA code. Once you have the library AND the transaction code applied, GA should properly track the order info. I can confirm that it does so the way I have my templates arranged.

  14. #14
    Client
    Join Date
    Feb 2010
    Posts
    42
    Squirrelcart version
    v3.0.1

    GA E-Commerce

    Hi coastalrugs, thanks for the quick reply. Right now I have this code in my store-main.tpl.php file

    <script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXXX-1']);
    _gaq.push(['_trackPageview']);

    (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>

    and in my checkout-done.tpl.php I have

    <script type="text/javascript">
    var _gaq = _gaq || [];

    _gaq.push(['_setAccount', 'UA-XXXXXX-1']);
    _gaq.push(['_trackPageview']);
    _gaq.push(['_addTrans',
    '<?php print $SC['order_placed']['number'] ?>', // order ID - required
    'Your Name Here', // affiliation or store name
    '<?php print $SC['order_placed']['grand_total'] ?>', // total - required
    '<?php print $SC['order_placed']['tax_total'] ?>', // tax
    '<?php print $SC['order_placed']['shipping_total'] ?>', // shipping
    '<?php print htmlentities($SC['order_placed']['address']['Bill_City'],ENT_QUOTES) ?>', // city
    '<?php print htmlentities($SC['order_placed']['address']['Bill_State_Name'],ENT_QUOTES) ?>', // state or province
    '<?php print htmlentities($SC['order_placed']['address']['Bill_Country_Name'],ENT_QUOTES) ?>' // country
    ]);
    <?php foreach($SC['order_placed']['items'] as $item) { ?>
    _gaq.push(['_addItem',
    '<?php print $SC['order_placed']['number'] ?>', // order ID - required
    '<?php print $item['record_number'].'-'.htmlentities($item['SKU'],ENT_QUOTES).'-'.htmlentities($item['Product_Code'],ENT_QUOTES).'-'.$item['hash'] ?>', // SKU/code - required
    '<?php print htmlentities($item['Name'],ENT_QUOTES) ?>', // product name
    '<?php print $item['primary_category'].'-'.htmlspecialchars(get_field_val('Categories','Nam e','record_number = '.$item['primary_category']),ENT_QUOTES); ?>', // category or variation
    '<?php print $item['total'] ?>', // unit price - required
    '<?php print $item['qty'] ?>' // quantity - required
    ]);
    <?php } ?>
    _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>


    Any suggestions would help. Thanks a lot.

  15. #15
    Client coastalrugs's Avatar
    Join Date
    Dec 2007
    Posts
    171
    Squirrelcart version
    v3.2.0
    Yup, that's the code snippet I meant. That should work for you.

    If you find results are being doubled, you could get rid of the redundant:
    Code:
    _gaq.push(['_setAccount', 'UA-XXXXXX-1']);
    _gaq.push(['_trackPageview']);
    Although duplicated, it should not cause any harm. I think Google probably accounts for this possibility. I wouldn't worry about it too much. The first hurdle is just confirming that the code functions as expected. If you had to remove it from a location, I would from the checkout_done template. My store includes this redundant call and seems to be behaving just fine.

    You should now see traffic for all your store pages, as well as data specific to any completed orders (Well, you'll see tomorrow at least, once the Anayltics servers refresh their data)

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts