Implementing Google Analytics eCommerce Tracking with PDG Commerce
Are you looking for a way to track the success of your search engine marketing? Want to know which keywords are converting to sales? Want to find out where people are abandoning the shopping process? Well, Google Analytics just might be for you.
Google Analytics is a free tool available to anyone, regardless of whether or not you are utilizing Google AdWords. After signing up at http://www.google.com/analytics/, you are provided with a tracking code that can be inserted in any and all of your HTML pages — including dynamic pages created by PDG Commerce. The code, a sample of which appears below, can be accessed directly from within your Google Analytics account.
<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-XX”); pageTracker._trackPageview(); } catch(err) {}</script>
Unlike prior versions of Google Analytics, the newest version of the tracking code will automatically detect whether your user is on a secure or non-secure page within your site, and will load the remotely hosted javascript accordingly — thus preventing “not all items on this page are secure” errors.
When a user visits your page, this script will send information about the visitor to your Google Analytics account. Where did the user come from? What page did they visit before landing on this specific page? How long were they on the prior page? What keyword, either organic or PPC, led the user to your site? All of this and more will be available to you from within Google Analytics.
For your standard “catalog” pages within PDG Commerce, including the list categories page, a listing of products within a category, a “hard coded” search results page and a detailed item page, simply following the instructions provided by Google and pasting the code immediately inside of the within the corresponding PDG template is all you need to do to implement Google Analytics. The same will be true for any static pages within your site.
For pages that are being generated based on a form being posted to PDG Commerce, in which case the URL displayed in the browser is just simply “http://[your_site]/cgi-bin/commerce.cgi (or something similar…), a little trickery is required so that Google will know what page is actually being viewed. For example, when a user is on a PDG generated “Item” page (“preadd” link), entering a quantity and hitting “add to cart” posts the data to PDG Commerce. The resulting page does not include a unique identifier within the URL, as this same URL is also what would be displayed when the user enters a keyword and submits a search, proceeds to checkout, etc. To get around this, you can make a slight modification to the form that will make the distinction available to Google. When viewing your PDG Commerce “Item” template’s HTML source, you’ll see the HTML form. The first line of the form reads somethink like:
<form method="post" action="!---SCRIPT_NAME---">
When the page is being generated by PDG, the tag is replaced with the correct URL for the PDG Commerce executable (commerce.cgi or commerce.exe in most instances). When the user enters a quantity, selects an option or makes other changes within the page, this is the script that the data is posted to AND is the URL that would be displayed in the browser and recognized by Google. In order to help distinguish between all the pages that might show this same URL, we can add a “?[something]” following the tag. With a form post, the data following the ? is completely irrelevant to the webserver, so it won’t effect the functionality of the page, however the resulting URL displayed in the browser would include this additional text and help you identify precisely what page the visitor is on.
To apply a label of “basket” to the form on the ITEM page, you would modify the opening form to read:
<form method="post" action="!---SCRIPT_NAME---?basket">
Note that the label should identify what the subsequent page would be, thus why the form for adding to cart would be labeled “?basket”. Similarly, the form on your checkout template would need to be labeled “payment” or “verify” so that when the user submits and lands on the final payment page, the URL would display appropriately. For a search form, you might make your label “?search” (note this would apply to any PDG search forms on your site, regardless of whether they are on PDG generated dynamic pages, or on your own static pages).
The last step of implementing Google Analytics with PDG Commerce involves the “ecommerce tracking” capability of Google Analytics. This specific capability allows you to not only track a users progress as they navigate through the site, but will even post the purchase information from the user’s purchase — thus enabling a world of possibilities for tracking your ROI (return on investment) from your search efforts.
This script, similar to affiliate tracking scripts that post data back to affiliate softwares/programs, goes on the final “thank you” page that the user sees after their payment has been accepted. This would be the CreditAccept template for “online” transactions (e.g. online credit card, online check, PayPal Express Checkout, etc.), or the ThankYou template for “offline” transactions (e.g. user selected “mail in payment” or “bill my account”).
This code, unlike the generic tracking code above, does need to be formatted specifically for PDG Commerce. Through the work and effort of members of the PDG support staff and members of the PDG Message Board communinity, we’ve provided a sample of the script below. This script should be inserted just inside the </body> tag within your template, FOLLOWING the generic tracking code discussed above).
Note: Some merchants have reported trouble when copying and pasting from the sample below with WordPress (blog software) apparently converting some of the characters into upper ASCII characters. We’ve provided a downloadable text document with the proper code at http://www.pdgsoft.com/msg/google_ecommerce_tracking_code.zip.
<script type="text/javascript">
pageTracker._addTrans(
"!–––INVOICE–––",
"Your_Store_Name",
"!–––NO_FORMAT_TOTAL–––",
"!–––NO_FORMAT_TAX–––",
"!–––NO_FORMAT_SHIPPING_TOTAL–––",
"!–––BILLCITY–––",
"!–––BILLSTATE–––",
"!–––BILLCOUNTRY–––"
);
<!–––BEGIN_REPEAT–––>
pageTracker._addItem(
"!–––INVOICE–––",
"!–––SKU–––",
"!–––DESC–––",
"!–––PRODUCT_CATEGORY_CODE–––",
"!–––NO_FORMAT_PRICE_WITH_ITEM_DISC–––",
"!–––QTY–––"
);
<!–––END_REPEAT–––>
pageTracker._trackTrans();
</script>
You’ll need to remove any breaks within this example, so that each “pageTracker” is on its own line.
Also, note that all of the examples provided do not include a Google Analytics account name/#. Within each example, you would need to replace the default example with your own Google Analytics account number.
We hope this is helpful for some of users looking to get a grasp on their ad spending, incoming traffic and marketing ROI!
PDG
UPDATE 2/27/09
Just wanted to let folks know that the code provided in the original post had been modified today, Feb. 27, 2009 to accommodate for some new PDG tags designed specifically for Google Analytics. If you previously used what was provided in the post, you might want to update your templates with the new data to ensure correct operation. A huge thanks goes out to our friends at http://www.stickeen.com/ who have helped tremendously in identifying and correcting some problems encountered with the initial code. Thanks for your help guys!

View Cart
RSS Feed
I just want to let you know that I have benefited from the information here. Thanks a lot….
Having a little problem with part of the tracking code. When I put in the repeat tags ” and ” Dreamweaver reports syntax errors. So I removed the brackets and added quotes and that took away the syntax errors but seemed to break the tracking code.
I could easily just copy and paste verbatim (except for the account number of course) but if it does not work, I muck up another day of tracking in Google which I would like to avoid.
So, is verbatim correct and I should ignore the syntax errors, or is there a problem with the posted code?
Thanks for providing this though!
DaK
Copying and pasting verbatim has been reported as successful on a number of occasions with the code listed currently. The initial code had some issues with non-traditional quotes inserted by the blog software, however we went back and ASCII encoded everything to make sure it displays properly.
If you want, you might consider adding the “new” code to your ThankYou.html template and set up a “test” payment method that doesn’t use any live processing. If your existing payment methods use live processing, they’ll continue to use the unedited CreditAccept.html template. Do your own test transaction and see if it reported successfully inside of Analytics. If so, you can then utilize the same code on your CreditAccept.html templates for your real transactions. Although it might skew your Analytics slighly, due to the test transaction, it might be worth making sure you don’t muck up your existing tracking efforts. Also, don’t forget to remove your test payment method!!!
Ok, didn’t work… but I think I might know the cause as I might be just a little thick… And just in case others are getting confused as well.
My current analytic code is a two piece javascript with the 2nd piece of script looking like:
try {
var pageTracker = _gat._getTracker(“accountNumber”);
pageTracker._trackPageview();
} catch(err) {}
I think my problem is that I’m just adding the code you provided as another javascript when I should be adding it to the second javascript that Google provides (if that makes sense) When it’s not working, I have 3 total Google scripts when I should only have 2?
If this is the case, should I also include this line “} catch(err) {}” which is in my 2nd Google generated code but not in the example you provide?
Sorry guys, I can usually just figure this stuff out.
Thanks,
DaK
DaK,
Combining your 2nd Google provided script with the ecommerce tracking script should do the trick. As you mentioned, the “standard” page tracking script (e.g. Google script #2) has some functions that are also repeated in the ecommerce tracking script. Another option is to remove the “repeated” functions from the ecommerce tracking script (e.g. the 2nd and 3rd lines in the ecommerce tracking script above — “var pageTracker = _gat._getTracker(“??–???????–??”);” and “pageTracker._trackPageview();”). You’ll still have 3 scripts, however the repeated fucntions will have been removed. The latter might be preferred for PDG users who are utilizing a “skin set”, where the 2nd script is automatically being populated on every page and can’t easily be combined with the ecommerce tracking script on the final thank you/credit accept template. For the sake of users copying and pasting these examples from this page, we have already made this change to the samples listed above.
Also, leaving the “} catch(err) {}” line does appear to be a good idea, as this added function of the Google script helps prevent page load errors in the event that the Google server was ever down or slow to respond (see http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=64711e0db5aae713&hl=en for details). This change has also been introduced into the sample code in the original post above.
Also, in the process of double-checking the code provided in the original post, we did identify an error within the initial javascript example. An extra “< " had been included in the URL reference to the Google script. It has been corrected, so copying and pasting from the examples in the original post should work fine. Fortunately, it appears most users had copied and pasted the correct Google provided code directly from within their Analytics account, so this had not been caught previously.
If anyone prefers to just pull down a text file with the code and simple instructions, you may do so from http://www.pdgsoft.com/msg/google_analytics.txt .
PDG
Will this work with Cart?
and is the message board gone?
Yes, this functionality is available for both PDG Commerce and PDG Shopping Cart. You will, though, need to be on the latest patch in order to have support for some of the new tags that were introduced to accommodate the Google scripts.
Also, Message Board is very much “alive and well”. You can access it from the navigation menu on the PDG site by selecting “services and support” -> “technical support” -> “message board”.
I liked the post, thanks…
This is very useful to know, but what should the Goal Page URL be?
Assuming that the “goal” for you is a successful completed transaction, the Goal Page URL would be the URL that you entered for the form post on the “Verify.html” template (e.g. probably something like “https://www.yoursite.com/cgi-bin/commerce.cgi?thankyou). Refer to paragraphs 6-9 in the original post for host to adjust the form posts on your pages in order to introduce a unique URL for the subsequent pages that are being generated by PDG Commerce.
For some reason, all the PDG variables, ex: !–––INVOICE––– aren’t being replaced by the order’s values.
What could this be?
Try opening the template and verifying that no “special” font/characters are being inserted by your HTML editor. We’ve confirmed with many, many users that the code above works, however we often find in situations that it isn’t working correctly that the HTML editor being used is replacing the standard dash (-) or exclamation point (!) with a non HTML recognized character. Make sure that all PDG tags have a standard – and ! via NotePad or something similar and give it another whirl. If you’re still having trouble, you might want to reach out to our technical support staff to have them take a look and verify your set up.
Re: Assuming that the “goal” for you is a successful completed transaction, the Goal Page URL would be the URL that you entered for the form post on the “Verify.html” template (e.g. probably something like “https://www.yoursite.com/cgi-bin/commerce.cgi?thankyou). Refer to paragraphs 6-9 in the original post for host to adjust the form posts on your pages in order to introduce a unique URL for the subsequent pages that are being generated by PDG Commerce.
Using this technique, this variable will appear even when the transaction is incomplete – eg:bad CC num
Does anyone know the trick to get the dollar amounts to show up and not just the fact that a transaction was logged?
Here is my issue…
http://msgboard.pdgsoft.com/cgi/yabb/YaBB.cgi?num=1276202217/0#5
When you go through a transaction and are viewing the final “thankyou” page, do you see the script when you view the source of the page? Is it populated (e.g. have the !—TAGS— been replaced with actual data)?