Add product data to Google Analytics tracking
Printed From: ProductCart E-Commerce Solutions
Category: ProductCart
Forum Name: Customizing ProductCart
Forum Description: Exchange messages with other users that are customizing ProductCart.
URL: https://forum.productcart.com/forum_posts.asp?TID=5808
Printed Date: 20-February-2025 at 1:29am Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com
Topic: Add product data to Google Analytics tracking
Posted By: Brett
Subject: Add product data to Google Analytics tracking
Date Posted: 27-February-2014 at 7:31pm
This may be included in a recent update, as we're still on 4.2b. On orderCompleteTracking.asp, you can find this comment:
' Advanced users: you use any of the following variables in your tracking code: ' pOID: This variable holds the order number from the ProductCart database ' pOrderNumber: The order number shown to customer ' ptotal: The order total amount ' pOrderStatus: An integer representing the status of the order
|
We already had some order data being tracked in GA (Google Analytics), but it was basically just the Order ID and total price. However, wanted to take full advantage of GA's tracking functions, and track tax, shipping charges, geographical data, and product information. This actually turned out to be easier than I expected. We modified our orderCompleteTracking like so:
<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-9999999-1']); _gaq.push(['_trackPageview']); _gaq.push(['_addTrans', '<%=pOrderNumber%>', // order ID - required 'Liftchair.com', // affiliation or store name '<%=ptotal%>', // total - required '<%=ptaxAmount%>', // tax '<%=Postage%>', // shipping '<%=pcity%>', // city '<%=pstate%>', // state or province '<%=pcountryCode%>' // country ]); <% for each orderProduct in orderProducts %> _gaq.push(['_addItem', '<%=pOrderNumber%>', // transaction ID - necessary to associate item with transaction '<%=orderProduct.item("sku")%>', // SKU/code - required '<%=orderProduct.item("name")%>', // product name - necessary to associate revenue with product '<%=orderProduct.item("category")%>', // category or variation '<%=orderProduct.item("price")%>', // unit price - required '<%=orderProduct.item("quantity")%>' // quantity - required ]); <% next %> _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
|
All of the new information for the _addTrans was already on the orderComplete.asp page, so it was just a matter of finding the variables (this may not work for countries that use VAT instead of Tax).
For the product information, I had to make a couple changes to orderComplete.asp:
At the beginning of the file I defined a new array to hold ordered product data (i don't know if it needs to go in a certain place, but i put it under all of the other dim statements as that seemed to make sense):
' // BRETT Add Product SKUs to GA Order Tracking START dim orderProducts() ' // BRETT Add Product SKUs to GA Order Tracking END
|
Then I set up this block of code to create an array of dictionary items holding all of the product data. I also had to pull the category from the database as it is not included in orderComplete.asp by default:
' BRETT Get default category query START queryCategories="SELECT categories_products.idCategory, categories_products.defaultCategory, categories.categoryDesc FROM categories_products LEFT JOIN categories ON categories_products.idCategory=categories.idCategory WHERE idProduct=" & pidProduct set rsCategories=server.CreateObject("ADODB.RecordSet") set rsCategories=conntemp.execute(queryCategories) if rsCategories.EOF then set rsCategories = nothing else categoriesArray=rsCategories.getRows() categoriesArrayCount=ubound(categoriesArray,2) set rsCategories = nothing end if pIdCategory = categoriesArray(0,0) pCategoryName = categoriesArray(2,0) for currentCat=0 to categoriesArrayCount if categoriesArray(1, currentCat) = 1 then pIdCategory = categoriesArray(0, currentCat) pCategoryName = Server.HTMLEncode(categoriesArray(2, currentCat)) end if next ' BRETT Get default category query END
' // BRETT Add Product SKUs to GA Order Tracking START redim preserve orderProducts(productCount) set orderProducts(productCount) = Server.CreateObject("Scripting.Dictionary")
orderProducts(productCount).Add "sku", pSku orderProducts(productCount).Add "name", pdescription orderProducts(productCount).Add "category", pCategoryName orderProducts(productCount).Add "price", punitPrice orderProducts(productCount).Add "quantity", pquantity
productCount = productCount + 1 ' // BRETT Add Product SKUs to GA Order Tracking END
|
I put that block of code right after this stuff:
'// Product Options Arrays pcv_strSelectedOptions = rsOrdObj("pcPrdOrd_SelectedOptions") ' Column 11 pcv_strOptionsPriceArray = rsOrdObj("pcPrdOrd_OptionsPriceArray") ' Column 25 pcv_strOptionsArray = rsOrdObj("pcPrdOrd_OptionsArray") ' Column 4 pcPrdOrd_BundledDisc=rsOrdObj("pcPrdOrd_BundledDisc") pGWText=rsOrdObj("pcPO_GWNote") pprocessDate=ShowDateFrmt(pprocessDate) pIdConfigSession=trim(pidconfigSession)
|
...and right before this stuff...
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' START: Get the total Price of all options '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pOpPrices=0 dim pcv_tmpOptionLoopCounter, pcArray_TmpCounter
|
So the whole section of the page should look like:
'// Product Options Arrays pcv_strSelectedOptions = rsOrdObj("pcPrdOrd_SelectedOptions") ' Column 11 pcv_strOptionsPriceArray = rsOrdObj("pcPrdOrd_OptionsPriceArray") ' Column 25 pcv_strOptionsArray = rsOrdObj("pcPrdOrd_OptionsArray") ' Column 4 pcPrdOrd_BundledDisc=rsOrdObj("pcPrdOrd_BundledDisc") pGWText=rsOrdObj("pcPO_GWNote") pprocessDate=ShowDateFrmt(pprocessDate) pIdConfigSession=trim(pidconfigSession) ' BRETT Get default category query START queryCategories="SELECT categories_products.idCategory, categories_products.defaultCategory, categories.categoryDesc FROM categories_products LEFT JOIN categories ON categories_products.idCategory=categories.idCategory WHERE idProduct=" & pidProduct set rsCategories=server.CreateObject("ADODB.RecordSet") set rsCategories=conntemp.execute(queryCategories) if rsCategories.EOF then set rsCategories = nothing else categoriesArray=rsCategories.getRows() categoriesArrayCount=ubound(categoriesArray,2) set rsCategories = nothing end if pIdCategory = categoriesArray(0,0) pCategoryName = categoriesArray(2,0) for currentCat=0 to categoriesArrayCount if categoriesArray(1, currentCat) = 1 then pIdCategory = categoriesArray(0, currentCat) pCategoryName = Server.HTMLEncode(categoriesArray(2, currentCat)) end if next ' BRETT Get default category query END
' // BRETT Add Product SKUs to GA Order Tracking START redim preserve orderProducts(productCount) set orderProducts(productCount) = Server.CreateObject("Scripting.Dictionary")
orderProducts(productCount).Add "sku", pSku orderProducts(productCount).Add "name", pdescription orderProducts(productCount).Add "category", pCategoryName orderProducts(productCount).Add "price", punitprice orderProducts(productCount).Add "quantity", pquantity
productCount = productCount + 1 ' // BRETT Add Product SKUs to GA Order Tracking END
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' START: Get the total Price of all options '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
You'll see something in there about defaultCategory. We noticed there was no good way to set a "default" product category for breadcrumbs, canonicals, and other stuff. I just added a new defaultCategory column to categories_products and changed the modifyProduct.asp page on the dashboard to allow us to set a default category for each product.
This helped as, by default, productcart would use the first category to which the product was assigned for the breadcrumbs, which can sometimes be problematic.
However, you can probably replace that whole "Get default category" with a simple query to just pull the category name of the first assigned category for the product:
' BRETT Get default category query START queryCategories="SELECT categories_products.idCategory, categories.categoryDesc FROM categories_products LEFT JOIN categories ON categories_products.idCategory=categories.idCategory WHERE idProduct=" & pidProduct set rsCategories=server.CreateObject("ADODB.RecordSet") set rsCategories=conntemp.execute(queryCategories) if rsCategories.EOF then set rsCategories = nothing set categoryName = "Unable to locate category." else categoriesArray=rsCategories.getRows() set rsCategories = nothing pIdCategory = categoriesArray(0,0) pCategoryName = categoriesArray(1,0) end if ' BRETT Get default category query END
|
I'm not sure if this will be useful for anyone else, but I figured I'd post it here as it was a fairly simple modification. If anyone notices any errors / vulnerabilities in the code please let me know.
|
Replies:
Posted By: Guests
Date Posted: 27-February-2014 at 11:04pm
Nice work, Brett!
Very gracious of you to share this.
I'm curious what insights you plan to glean from this data, though. Mind sharing that side of it?
|
Posted By: Brett
Date Posted: 27-February-2014 at 11:06pm
I'm not sure how to do this in Analytics yet, but I want to be able to tell where sales for certain products are coming from, as far as keywords, referring sites, and what pages they went through to get to the product.
|
Posted By: Guests
Date Posted: 28-February-2014 at 12:49am
Thanks, Brett. I think I could see how that could be valuable data perhaps vis a vis targeting ads. Excellent first step in the project. If you don't have the data, you can't mine it, right? I'd like to think more about it.
Meanwhile, a couple of observations: 1) You might want to remove your Google account id from the code above (using the edit post option). Probably no harm in having your domain in there too, but I'll leave that up to you. 2) Having seen that in your posted code I peeped your site to see if I could quickly glean how this data might be interesting for you .... and I noticed your coupons page. I've been giving a lot of thought in the back of my busy mind about how to leverage exactly such a page for social media and other marketing angles. Incidentally, you might find that our Discount Codes before Checkout extension would play perfectly with your objective here.
|
Posted By: Brett
Date Posted: 01-March-2014 at 12:33am
Whoops! Thanks for the heads up on GA account. I'll get in touch with you regarding coupons page. We love it, and probably 80% of our customers use the coupons. Though maybe they'd still buy and not use coupons if we didn't have them, who knows lol.
Discount codes before checkout is something we've been wanting for a long time. I will definitely ask you about this (hopefully) tomorrow.
|
Posted By: whizzinpc
Date Posted: 03-March-2014 at 12:12am
Thanks for sharing Brett. This is very useful.
|
|