Using FoxyCart 2.0? Google Analytics is now a configuration option within your store's administration. Take a look at Store Analytics for more information.
You probably don't want to be at this page! Unless you're on a very old Foxy account, you'll want to use these docs over here. It's considerably easier and more powerful in our latest release.
SKU
parameter in Google Analytics, which is the code
parameter in FoxyCart. If your products do not include a code
value (as passed to FoxyCart) FoxyCart will attempt to generate one from the name
attribute, but FoxyCart will not take into account duplicate product names, which can clobber your data. If you are not using the code
parameter for your products, this Google Analytics tracking may not work correctly.https://your_foxycart_domain
and not http://your_foxycart_domain
.foxycart.colorbox.js
(the default behavior) or foxycart.js
in your website template (do not include this in your cart, checkout, or receipt template). If you don't know what you're using, you're probably using foxycart.colorbox.js
and don't have to worry, but you can view your template source to confirm. The code below assumes that fcc
has been declared as a new FC.client
, so if you're using foxycart.raw.js
you'll need to create fcc
or modify the FC.ga
object accordingly.ga.js
and not urchin.js
. If you need to use urchin.js
it should be similar to this, but the urchin.js
Google Analytics code is deprecated at this point so you'll have to adapt these instructions as necessary.cart=checkout
) links originating from outside the site being tracked by Google Analytics. For instance, if you have cart=checkout
links in an email, the referrer or campaign may not be maintained. If this is an issue for you please let us know.On the cart, change this line: window.location.hash = fc_json.custom_fields['ga']; to this: window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); And this line: var href_link = $(this).attr('href') + fc_json.custom_fields['ga']; to this: var href_link = $(this).attr('href') + fc_json.custom_fields['ga'].replace( /\&/g, '&' ); On the checkout, change this line: window.location.hash = fc_json.custom_fields['ga']; to this: window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); On the receipt, change this: window.location.hash = fc_json.custom_fields['ga']; to: window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' );
This has been changed in all the examples below.
example.foxycart.com
, select the “Multiple top-level domains” radio button, as shown on the screenshot to the right.secure.example.com
, select the “One domain with multiple subdomains” radio button.</head>
tag.” Do not put this code on your FoxyCart cart, checkout, or receipt templates.fcsid
to the “Exclude URL Query Parameters” in your analytics settings. Details here./receipt
/cart
for the URL and “Cart” for the Name. (You won't have this step if you are using “direct to checkout” with the parameter “cart=checkout”). Do not check the “required step” checkbox, as there are situations where the cart can be skipped./checkout
for the URL and “Checkout” for the Name.
If you're using PayPal Express Checkout, you can create two additional goals. We'll call them “Sale (PayPal as Checkout)” and “Sale (PayPal as Payment Method)”. Create them the same as above, but for “Sale (PayPal as Checkout)” set the funnel like this:
/cart
, Cart/paypal_checkout
, PayPal (Checkout)/checkout
, Checkout (Confirmation)
And for “Sale (PayPal as Payment Method)” set the funnel like this:
/cart
, Cart/checkout
, Checkout/paypal_payment
, PayPal (Payment Method)Proceed to the next section.
<script type="text/javascript" charset="utf-8"> fcc.events.cart.preprocess.add(function(e, arr) { if (arr['cart'] == 'checkout' || arr['cart'] == 'updateinfo' || arr['output'] == 'json') { return true; } if (arr['cart'] == 'checkout_paypal_express') { _gaq.push(['_trackPageview', '/paypal_checkout']); return true; } _gaq.push(['_trackPageview', '/cart']); return true; }); fcc.events.cart.process.add_pre(function(e, arr) { var pageTracker = _gat._getTrackerByName(); jQuery.getJSON('https://' + storedomain + '/cart?' + fcc.session_get() + '&h:ga=' + escape(pageTracker._getLinkerUrl('', true)) + '&output=json&callback=?', function(data){}); return true; }); </script>
This section includes “the easy way” for getting Google Analytics going. It doesn't include the full “why”, but if you just want to copy and paste, follow the steps below. Copy the code, paste it into the appropriate template (either in FoxyCart's admin directly, or on your system that FoxyCart is template-caching), and change the XXXXXXXX-X
. NOTE: YOU MUST CHANGE THE XXXXXXXX-X IN ALL LOCATIONS TO YOUR UA CODE!
<script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } if (typeof(fc_json.custom_fields['ga']) != "undefined") { $('a.fc_link_nav').each(function(){ var href_link = $(this).attr('href') + fc_json.custom_fields['ga'].replace( /\&/g, '&' ); $(this).attr('href', href_link); }); } </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); _gaq.push(['_setDomainName', 'none']); _gaq.push(['_setAllowLinker', true]); (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> <script type="text/javascript" charset="utf-8"> jQuery(function($){ $('a.fc_cart_checkout_paypal').click(function(e){ // e.preventDefault(); // TODO _gaq.push(['_trackPageview', '/paypal_checkout']); // setTimeout('document.location = "' + this.href + '";', 200); // TODO }); }); </script>
<script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); _gaq.push(['_setDomainName', 'none']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setAllowAnchor', true]); _gaq.push(['_trackPageview', '/checkout']); (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> <script type="text/javascript" charset="utf-8"> function ga_tracker() { if (typeof(fc_json.custom_fields['ga']) != "undefined" && jQuery('#fc_payment_method_paypal').is(":checked") == true) { _gaq.push(['_trackPageview', '/paypal_payment']); // setTimeout('return true;', 250); // TODO } } FC.checkout.overload('validateAndSubmit', 'ga_tracker', null); </script>
<script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); _gaq.push(['_setDomainName', 'none']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setAllowAnchor', true]); _gaq.push(['_trackPageview', '/receipt']); (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> ^^receipt_only_begin^^ ^^analytics_google_ga_async^^ ^^receipt_only_end^^
Once you've replaced all the XXXXXXXX-X
in the copy/paste code above and got it successfully saved as your FoxyCart templates, proceed to test things out.
This section includes the details that “the easy way” above skips over. If you've already done the above “easy way” section you can safely skip down to the testing section.
</body>
tag in your cart template. This can be directly in your FoxyCart admin or (preferably) in your cart template on your server that you will load into FoxyCart via FoxyCart's template caching functionality._gaq.push(['_trackPageview']);
line. We're tracking it manually already. (It's not very reliable when called inside an iframe loaded from a separate domain, in case you're wondering.)<script>
tag you just added on your cart template:<script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } if (typeof(fc_json.custom_fields['ga']) != "undefined") { $('a.fc_link_nav').each(function(){ var href_link = $(this).attr('href') + fc_json.custom_fields['ga'].replace( /\&/g, '&' ); $(this).attr('href', href_link); }); } </script>
</script>
tag on your cart template. <script type="text/javascript" charset="utf-8"> jQuery(function($){ $('a.fc_cart_checkout_paypal').click(function(e){ // e.preventDefault(); // TODO _gaq.push(['_trackPageview', '/paypal_checkout']); // setTimeout('document.location = "' + this.href + '";', 200); // TODO }); }); </script>
</body>
tag in your checkout template. This can be directly in your FoxyCart admin or (preferably) in your checkout template on your server that you will load into FoxyCart via FoxyCart's template caching functionality._gaq.push(['_trackPageview']);
and change it to this:
_gaq.push(['_setAllowAnchor', true]); _gaq.push(['_trackPageview', '/checkout']);
Notice that we added a line there. That's very important.
<script>
tag you just added on your cart template: <script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } </script>
</script>
tag on your cart template. <script type="text/javascript" charset="utf-8"> function ga_tracker() { if (typeof(fc_json.custom_fields['ga']) != "undefined" && jQuery('#fc_payment_method_paypal').is(":checked") == true) { _gaq.push(['_trackPageview', '/paypal_payment']); // setTimeout('return true;', 250); // TODO } } FC.checkout.overload('validateAndSubmit', 'ga_tracker', null); </script>
<script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXX-X']); _gaq.push(['_setDomainName', 'none']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setAllowAnchor', true]); _gaq.push(['_trackPageview', '/checkout']); (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> <script type="text/javascript" charset="utf-8"> function ga_tracker() { if (jQuery('#fc_payment_method_paypal').is(":checked") == true || (jQuery('#fc_payment_method_paypal_container').length > 0 && jQuery('#fc_payment_method_paypal_container').find('input[type=radio]').length == 0)) { _gaq.push(['_trackPageview', '/paypal_payment']); // setTimeout('return true;', 250); // TODO } } FC.checkout.overload('validateAndSubmit', 'ga_tracker', null); </script>
You could copy this entire block, but you'll need to replace the XXXXXXX-X with your own account number.
_gaq.push(['_trackPageview', '/checkout']);
to _gaq.push(['_trackPageview', '/receipt']);
^^receipt_only_begin^^ ^^analytics_google_ga_async^^ ^^receipt_only_end^^
<script type="text/javascript" charset="utf-8"> if (window.location.hash.search(/utma/) == -1 && typeof(fc_json.custom_fields['ga']) != "undefined") { if (fc_json.custom_fields['ga'].length > 0) { window.location.hash = fc_json.custom_fields['ga'].replace( /\&/g, '&' ); } } </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXX-X']); _gaq.push(['_setDomainName', 'none']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setAllowAnchor', true]); _gaq.push(['_trackPageview', '/receipt']); (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> ^^receipt_only_begin^^ ^^analytics_google_ga_async^^ ^^receipt_only_end^^
You could copy this entire block, but you'll need to replace the XXXXXXX-X with your own account number.
_utm*
.It's often difficult to troubleshoot because it takes upwards of four hours for any transactions to appear in Google Analytics. So if you set up FoxyCart to be tracked in Google Analytics, and you try a few test transactions, it will take at least a few hours before you can see if things are working correctly. There's a faster way: you can look directly at the cookies set by Google Analytics.
GA sets cookies named utma
, utmb
, utmc
, and utmz
. Each has a “host” parameter as well as a value. See a detailed description of the information stored in these cookies.
The easiest way to approach this is to view the cookies directly in your browser. Clear out the cookies for your domain, making sure to delete all the utm*
cookies as well as any fcsid
cookies. (If you're using Firefox, you can install Chris Pederick's Web Developer Toolbar for Firefox, which is amazing, and allows you to clear cookies for a domain (among other things.) Make sure you clear out the utm*
cookies both for your domain and for your store's FoxyCart domain. So, example.com
and example.foxycart.com
. Otherwise you'll get goofy results.
Once you have your cookies cleared, load up a page with a FoxyCart add-to-cart link or form. See what cookies were set. You should see at least an
fcsid
cookie and 4 __utm*
cookies. Take a look at the data in __utma
. Now add something to your cart and proceed through to checkout. View your cookies again. Check the __utma
for the FoxyCart domain, then compare it to the __utma
at your domain. They should be identical, or close to identical. If they're radically different, it's not working. Take a look at the image to the right for a screenshot of what it looks like in Firefox on OS X. (Go Preferences → Privacy (Use constom settings for history) → Show Cookies. It'll actually show them to you as they're set, which is very handy.)
You can also take a close look at the value for the __utmz
cookie. This cookie encodes all the essential details of how a visitor came to the site, such as referring site or search keyword. This is the data that you want to see tracked all the way to your final order receipt page. That cookie should contain the same info for both your domain (like example.com
) and for your store's FoxyCart domain (like example.foxycart.com
).