The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

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?
  • fc_adamfc_adam FoxyCart Team

    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

    Essentially you'll need to mimic the code that's Google expects for the transaction information on the receipt:

    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">
    		"85154893",		// Order ID
    		"Store Name",		// Affiliation
    		"24.7",		// Total
    		"0",		// Tax
    		"12.7",		// Shipping
    		"Town",		// City
    		"NY",		// State
    		"US"		// Country
    		"85154893",		// Order ID
    		"SKU123",		// SKU
    		"My Product Name",		// Product Name
    		"large",		// Category
    		"2",		// Price
    		"6"		// Quantity

    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:
    <script type="text/javascript">
            "{{ 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 %}
            "{{ order_id }}",     // Order ID
            "{{ item.code }}",       // SKU
            "{{ }}",      // Product Name
            {% for option in item.options -%}
            {% if == 'Type' -%}
            "{{ option.value }}",        // Category
            {% endif -%}
            {% endfor -%}
            "{{ item.price_total}}",        // Price
            "{{ item.quantity }}"     // Quantity
     {% endfor %}
  • fc_adamfc_adam FoxyCart Team

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