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 (as of v060) will attempt to generate one from the name
attribute. Prior to v060 a code
value should be considered required for Google Analytics to properly track the items in your transactions.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.http:
href
s will work for add-to-cart requests, the redirect from http
to https
will cause problems for GA. Ensure that all link href
and form action
attributes go to https
URLs.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.
JSON(P)
based cart, or are otherwise doing something custom you may need to modify this code, depending on your needs. As always, test test test.
If your cart and checkout are at something like example.foxycart.com
, follow the steps below. If you're using a custom subdomain and your checkout is at something like secure.example.com
, skip to the next section.
<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"> try { var pageTracker = _gat._getTracker("UA-XXXXXXX-X"); pageTracker._setDomainName("none"); pageTracker._setAllowLinker(true); pageTracker._trackPageview(); } catch(err) {}</script>
Make sure to replace the XXXXXXX-X
bit with your account information. The important pieces are the following two lines, which should be above your trackPageview()
call:
pageTracker._setDomainName("none"); pageTracker._setAllowLinker(true);
If your cart and checkout are at a custom subdomain, something like secure.example.com
, follow the steps below. If you're using the default *.foxycart.com
domain, complete the section above and then skip this section.
yourdomain.tld
, www.yourdomain.tld
, secure.yourdomain.tld
) you'll need to change your tracking code slightly, as described in the Google Analytics docs. As of 20100108 Google actually allows you to select this when you get your tracking code. Under Step 1 choose the radio button that says “One domain with multiple subdomains”.XXXXXXX-X
and YOURDOMAIN.TLD
with your own account information). The important piece is the _setDomainName(“.YOURDOMAIN.TLD”)
bit (note the leading dot):<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"> try { var pageTracker = _gat._getTracker("UA-XXXXXXX-X"); pageTracker._setDomainName(".YOURDOMAIN.TLD"); pageTracker._trackPageview(); } catch(err) {}</script>
fcsid
to the “Exclude URL Query Parameters” in your analytics settings. (If you're using FoxyCart prior to v051, use PHPSESSID,fc_PHPSESSID
instead.) 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”). (Required step?… uh… I think you should check this box)/checkout
for the URL and “Checkout” for the Name.<script type="text/javascript" charset="utf-8"> jQuery(function($){ $('a.foxycart').click(function(){ if (!$(this).attr('href').match('cart=checkout')) { pageTracker._trackPageview('/cart'); } }); $('form.foxycart').submit(function(){ if (!$(this).serialize().match('cart=checkout')) { pageTracker._trackPageview('/cart'); } }); }); function fc_BuildFoxyCart() { // Add the Google Analytics data to the FoxyCart session $.getJSON('https://' + FoxyDomain + '/cart?cart=view' + fc_AddSession() + '&h:ga=' + escape(pageTracker._getLinkerUrl('', true)) + '&output=json&callback=?', function(data){}); } </script>
Note that if you're already using an fc_BuildFoxyCart()
function elsewhere you'll need to add this .getJSON
line to that function, or tie it in wherever appropriate. The key is that we want to keep the FoxyCart session up to date with the GA session data. Also note that this line replaces the need to set up crosslinks/forms as Google describes in this article on tracking multiple domains. The JSONP code that we'll use throughout the cart-checkout-receipt process ensures that the Foxycart session has the correct GA session values.
</body>
tag on your CART template. Do not include the GA script calls on your cart template or you'll register that page twice (the visit to the cart page is tracked by a Javascript call on your product page): <script type="text/javascript" charset="utf-8"> jQuery(function($){ if (fc_json.custom_fields['ga']) { $('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>
jQuery
piece or the fc_BuildFoxyCart
piece) plus additional code that captures the tracking parameters. Don't grab the code from the “CART” section above. The example below shows what you'll need on your checkout template.pageTracker._trackPageview();
to pageTracker._trackPageview(“/checkout”);
pageTracker._trackPageview(“/checkout”);
line, add this:pageTracker._setAllowLinker(true); pageTracker._setAllowAnchor(true);
// Ensure the GA info is in the URL // 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, '&' ); } }
XXXXXXX-X
with your appropriate Google Analytics account info): .YOURDOMAIN.com
with the appropriate code (retain the leading dot – it's essential!):<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"> // Ensure the GA info is in the URL // 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, '&' ); } } try { var pageTracker = _gat._getTracker("UA-XXXXXXX-X"); pageTracker._setDomainName(".YOURDOMAIN.com"); pageTracker._setAllowLinker(true); pageTracker._setAllowAnchor(true); pageTracker._trackPageview('/checkout'); } catch(err) {}</script> </body> </html>
*.foxycart.com
domain:<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"> // Ensure the GA info is in the URL // 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, '&' ); } } try { var pageTracker = _gat._getTracker("UA-XXXXXXX-X"); pageTracker._setDomainName("none"); pageTracker._setAllowLinker(true); pageTracker._setAllowAnchor(true); pageTracker._trackPageview('/checkout'); } catch(err) {}</script> </body> </html>
window.location.hash
bit. This is important.pageTracker._trackPageview();
to pageTracker._trackPageview(“/receipt”);
^^analytics_google_ga^^
. This generates code that tracks the sale and actually tracks the ecommerce transaction. (If you're using urchin.js
, use ^^analytics_google_urchin^^
.) Because your store's receipts can be revisited by your customers, you'll need to wrap the ^^analytics_google_ga^^
(or _urchin
) inside additional placeholders to ensure it only gets loaded on the initial receipt view. These placeholders are described on the placeholders documentation. It should look like this:^^receipt_only_begin^^ ^^analytics_google_ga^^ ^^receipt_only_end^^
Without these ^^receipt_only_
placeholders a single transaction could be logged multiple times, throwing off your data.
XXXXXXX-X
with your appropriate Google Analytics account info: .YOURDOMAIN.com
with the appropriate code (retain the leading dot – it's essential!):<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"> try { var pageTracker = _gat._getTracker("UA-XXXXXXX-X"); pageTracker._setDomainName(".YOURDOMAIN.com"); pageTracker._trackPageview('/receipt'); } catch(err) {}</script> ^^receipt_only_begin^^ ^^analytics_google_ga^^ ^^receipt_only_end^^ </body> </html>
*.foxycart.com
domain:<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"> try { var pageTracker = _gat._getTracker("UA-XXXXXXX-X"); pageTracker._setDomainName("none"); pageTracker._trackPageview('/receipt'); } catch(err) {}</script> ^^receipt_only_begin^^ ^^analytics_google_ga^^ ^^receipt_only_end^^ </body> </html>
^^analytics_google_ga^^
will output the cart array, as described here. GA will pick up on this automatically.^^receipt_only_begin^^
and ^^receipt_only_end^^
tags, as they aren't supported.code
parameter passed in to FoxyCart (which outputs as the SKU
in the Google Analytics tracking), they may not record correctly. Also, spaces in the code
(SKU
) parameter may cause problems. FoxyCart attempts to automatically set a SKU based on the name
attribute if no code
value is present, but we recommend setting explicit code values if you care.If you follow the instructions above, you'll have no problems. If you do, check your code and then review these tips for troubleshooting.
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 four 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.
You'll need to be able to view cookies (and clear or reset cookies) in your web browser. If you're using Firefox, you can install Chris Pederick's Web Developer extension for Firefox: http://chrispederick.com/work/web-developer/.
Visit your product order page. From the Web Developer toolbar, select “View Cookie Information” from the Cookies mneu.
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 GA tracking code you add to your product page determines what the host value will be. The simplest pageTracker._trackPageview()
will give you .mysite.com
as a host parameter (with a leading dot). Adding pageTracker._setDomainName(“none”)
will give you www.mysite.com
as a host parameter if the domain is www.mysite.com
. You can check the GA cookies to make sure the “host” is what you expect.
Each cookie will have a value that begins with a “hash” that encrypts the host value. The domain hashing functionality in Google Analytics creates a hash value from your domain, and uses this number to check cookie integrity for visitors. If you have multiple sub-domains, such as example1.example.com and example2.example.com, and you want to track user behavior across both of these sub-domains, you would turn off domain hashing so that the cookie integrity check will not reject a user cookie coming from one domain to another. If you add pageTracker._setDomainName(“none”)
or pageTracker._setAllowHash(false)
to your GA tracking code, each cookie will have the integer “1” as its hash value. If you are tracking across multiple domains and you don't see “1” as the first integer in a GA cookie value, you may have a problem.
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.
Add your product to your cart and click through to Checkout. Check your cookie information again. You should see a new host (example.foxycart.com) and the utmz
value should contain the same tracking data that you saw in the utmz
cookie for your product page. If it does not, for example, if it shows utmcsr=(direct)
, something may be configured incorrectly or broken.
Keep in mind that cookies are persistent. If you are testing, you will need to “Delete Domain Cookies” on your checkout page and product page prior to initiating a new test.