How to add category to Google Analytics

happycloudhappycloud Member
in Help edited February 2013
I need to track the category in google ecommerce. I have tried adding a 'category' field to my add to cart form in hopes that it would be carried over but evidently that is for FC categories and require them to be setup in admin which I do not want. How would I go about modifying/adding data to this analytics code?
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @happycloud,

    Unless you want to build out the Google Analytics code for describing the order yourself, your best bet will be to use the FoxyCart categories - as that will be passed through to GA on the receipt with the "analytics_google_ga_async" placeholder.

    Is there a particular reason you don't want to use the FC categories?
  • Because we have a LOT. And we add them continuously so i don't want to have to add them to FC avery few days just for this. How would I go about overriding FC's code?
  • And am I even on the right path with the category field? is that the one that gets added to analytics?
  • fc_adamfc_adam FoxyCart Team
    @happycloud,

    Essentially you'll need to mimic the code that's Google expects for the transaction information on the receipt: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce

    You could do that either by adding some javascript in your template - looping through the products and calling the relevant function each time, or create the javascript using Twig directly in the template.

    If you're not planning on using FC's in-built categories, you'll need to give your category it's own unique name in the product add to cart, and then key off of that attribute when building out the ecommerce tracking on the receipt.

    This is what the the code looks like when included by the placeholder on the receipt:
    <script type="text/javascript" charset="utf-8">
    	_gaq.push(["_addTrans",
    		"85154893",		// Order ID
    		"Store Name",		// Affiliation
    		"24.7",		// Total
    		"0",		// Tax
    		"12.7",		// Shipping
    		"Town",		// City
    		"NY",		// State
    		"US"		// Country
    	]);
    	
    	_gaq.push(["_addItem",
    		"85154893",		// Order ID
    		"SKU123",		// SKU
    		"My Product Name",		// Product Name
    		"large",		// Category
    		"2",		// Price
    		"6"		// Quantity
    	]);
    						
    	_gaq.push(["_trackTrans"]);
    </script>
    

    Note that if you roll your own, you need to ensure that this sort of code is only run on the first time the receipt is loaded - otherwise if someone revisits their receipt, it will skew your analytics.
  • Awesome! I am posting what I did in case somebody else needs this. I passed the custom option 'type' to my cart that holds the category I want passed to GA. I then modified the bottom of my receipt template (the twig one) and removed the call to analytics and replaced it with:
    ^^receipt_only_begin^^
    <script type="text/javascript">
        _gaq.push(["_addTrans",
            "{{ order_id }}",     // Order ID
            "{{ store_name }}",       // Affiliation
            "{{ cart_order_total }}",     // Total
            "{{ receipt_tax }}",        // Tax
            "{{ cart_total_shipping }}",     // Shipping
            "{{ city }}",     // City
            "{{ region_code }}",       // State
            "{{ country_name }}"        // Country
        ]);
     {% for item in items %}
        _gaq.push(["_addItem",
            "{{ order_id }}",     // Order ID
            "{{ item.code }}",       // SKU
            "{{ item.name }}",      // Product Name
            {% for option in item.options -%}
            {% if option.name == 'Type' -%}
            "{{ option.value }}",        // Category
            {% endif -%}
            {% endfor -%}
            "{{ item.price_total}}",        // Price
            "{{ item.quantity }}"     // Quantity
        ]);
     {% endfor %}
        _gaq.push(["_trackTrans"]);
    
     </script>
    ^^receipt_only_end^^
    
  • fc_adamfc_adam FoxyCart Team
    @happycloud,

    Thanks for posting your solution!
Sign In or Register to comment.