Google Analytics Upgrade
Google Analytics Upgrade
Hello,
Google is upgrading the tracking code techniques, from "ga.js" version (which is built into Ablecommerce as a ConLib) to "analytics.js version".
Tracking transactions & other features can't be done by only using the regular tracking code, and needs to be customized in order to track transactions & other details.
This will require a full customization of the built in [[ConLib:GoogleAnalyticsWidget]].
Are you planning to upgrade this Conlib or at least offer a new one for the new Universal tracking code "analytics.js" ?
Thx
Google is upgrading the tracking code techniques, from "ga.js" version (which is built into Ablecommerce as a ConLib) to "analytics.js version".
Tracking transactions & other features can't be done by only using the regular tracking code, and needs to be customized in order to track transactions & other details.
This will require a full customization of the built in [[ConLib:GoogleAnalyticsWidget]].
Are you planning to upgrade this Conlib or at least offer a new one for the new Universal tracking code "analytics.js" ?
Thx
Never leave for tomorrow what you can do today.
Re: Google Analytics Upgrade
I am also looking to do this upgrade.
Re: Google Analytics Upgrade
I am also looking to do this and I hope there is some updated support for PayPal orders which my Google Analytics still refuses to recognize.
Re: Google Analytics Upgrade
Any dig into the code to do this them self by editing there footer and header?
- ImmortalLogic
- Lieutenant (LT)
- Posts: 61
- Joined: Tue May 11, 2010 9:29 am
Re: Google Analytics Upgrade
I just found this post and it's the exact same issue we are experiencing after having Google push us to update to the new Universal tracking code "analytics.js". Will there be any updates to assist us in setting up the new tracking code? Could someone please help?
Re: Google Analytics Upgrade
Mazher, Katie, Logan to the rescue?
Or someone at least tell us that Able won't help on this and we should just pay someone to do this for us.
Please stop leaving this thread un-replied. It makes this cart look like it doesn't care about it's forum.
Or someone at least tell us that Able won't help on this and we should just pay someone to do this for us.
Please stop leaving this thread un-replied. It makes this cart look like it doesn't care about it's forum.
- ImmortalLogic
- Lieutenant (LT)
- Posts: 61
- Joined: Tue May 11, 2010 9:29 am
Re: Google Analytics Upgrade
I went directly to Google for assistance and they got us in touch with one of their tech programmers that helped us with the code. I had started with http://misterphilip.com/universal-analy ... /ecommerce and was able to get the hard coded data to appear in our Google Analytics. So I sent that and the GoogleAnalyticsWidget code, which showed the parameters used to pull the data out and send over to analytics previously, to their programmer.
This was the code that they sent back to me and it worked! (We didn’t have categories or store name before, and we personally didn’t need it now, so that doesn’t come through.)
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
// Create the tracker\
var orderid=parseInt(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_PageHeaderPanel").getElementsByTagName("h1")[0].innerHTML.replace("View Order","").replace("#","").trim());
var shipping=parseFloat(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_OrderTotalSummary1_Shipping").innerHTML.replace("$",""))
var total =parseFloat(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_TotalCharges").innerHTML.replace("$",""));
var tax =parseFloat(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_OrderTotalSummary1_Taxes").innerHTML.replace("$",""));
ga('create', UA-XXXXX-Y');
// Fire off a pageview
ga('send', 'pageview');
// Include the ecommerce plugin
ga('require', 'ecommerce', 'ecommerce.js');
// Initialize the transaction
ga('ecommerce:addTransaction', {
'id': orderid, // Transaction ID*
'affiliation': "Store Name ", // Store Name
'revenue': total, // Total
'shipping': shipping, // Shipping
'tax': tax // Tax
});
// Add a few items
var table=document.getElementById("ctl00_wpm_ReceiptPage_ctl02_ShipmentRepeater_ctl00_ShipmentItemsGrid")
for (var r = 1, n = table.rows.length; r < n; r++) {
ga('ecommerce:addItem', {
'id': orderid, // Transaction ID*
'sku': table.rows[r].cells[1].getElementsByTagName("span")[0].innerHTML, // Product SKU
'name': table.rows[r].cells[2].innerHTML, // Product Name*
'category': " ", // Product Category
'price': parseFloat(table.rows[r].cells[3].getElementsByTagName("span")[0].innerHTML.replace("$","")), // Price
'quantity': table.rows[r].cells[0].getElementsByTagName("span")[0].innerHTML // Quantity
});
}
// Send off the transaction
ga('ecommerce:send');
</script>
Hopefully this will help others, or at least get them started!
This was the code that they sent back to me and it worked! (We didn’t have categories or store name before, and we personally didn’t need it now, so that doesn’t come through.)
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
// Create the tracker\
var orderid=parseInt(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_PageHeaderPanel").getElementsByTagName("h1")[0].innerHTML.replace("View Order","").replace("#","").trim());
var shipping=parseFloat(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_OrderTotalSummary1_Shipping").innerHTML.replace("$",""))
var total =parseFloat(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_TotalCharges").innerHTML.replace("$",""));
var tax =parseFloat(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_OrderTotalSummary1_Taxes").innerHTML.replace("$",""));
ga('create', UA-XXXXX-Y');
// Fire off a pageview
ga('send', 'pageview');
// Include the ecommerce plugin
ga('require', 'ecommerce', 'ecommerce.js');
// Initialize the transaction
ga('ecommerce:addTransaction', {
'id': orderid, // Transaction ID*
'affiliation': "Store Name ", // Store Name
'revenue': total, // Total
'shipping': shipping, // Shipping
'tax': tax // Tax
});
// Add a few items
var table=document.getElementById("ctl00_wpm_ReceiptPage_ctl02_ShipmentRepeater_ctl00_ShipmentItemsGrid")
for (var r = 1, n = table.rows.length; r < n; r++) {
ga('ecommerce:addItem', {
'id': orderid, // Transaction ID*
'sku': table.rows[r].cells[1].getElementsByTagName("span")[0].innerHTML, // Product SKU
'name': table.rows[r].cells[2].innerHTML, // Product Name*
'category': " ", // Product Category
'price': parseFloat(table.rows[r].cells[3].getElementsByTagName("span")[0].innerHTML.replace("$","")), // Price
'quantity': table.rows[r].cells[0].getElementsByTagName("span")[0].innerHTML // Quantity
});
}
// Send off the transaction
ga('ecommerce:send');
</script>
Hopefully this will help others, or at least get them started!
Re: Google Analytics Upgrade
Thank you so much for providing this updated code!!
We had the upgrade of GA scheduled for our next release, but I notified the developers of this post and we're going to try and release a hot patch so no one will have to wait to upgrade.
We had the upgrade of GA scheduled for our next release, but I notified the developers of this post and we're going to try and release a hot patch so no one will have to wait to upgrade.
Thank you for choosing AbleCommerce!
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
- jmestep
- AbleCommerce Angel
- Posts: 8164
- Joined: Sun Feb 29, 2004 8:04 pm
- Location: Dayton, OH
- Contact:
Re: Google Analytics Upgrade
Be careful of code like this:
var orderid=parseInt(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_PageHeaderPanel").getElementsByTagName("h1")[0].innerHTML.replace("View Order","").replace("#","").trim());
If for some reason the html elements change, like moving them, adding, etc, those element names might change. This might change:
ctl00_wpm_ReceiptPage_ctl02_PageHeaderPanel
That is just something that .net does, it's not something Able does.
var orderid=parseInt(document.getElementById("ctl00_wpm_ReceiptPage_ctl02_PageHeaderPanel").getElementsByTagName("h1")[0].innerHTML.replace("View Order","").replace("#","").trim());
If for some reason the html elements change, like moving them, adding, etc, those element names might change. This might change:
ctl00_wpm_ReceiptPage_ctl02_PageHeaderPanel
That is just something that .net does, it's not something Able does.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx
Re: Google Analytics Upgrade
Thank you ImmortalLogic! Unfortunately I do not know how to implement this though. I was expecting an update to the GoogleAnalyticsWidget.ascx.cs but this is written like an ascx.cs file. What code does this replace?
When Judy tells me to be wary of a code I listen, so Katie, your "next release" will only update Able Gold correct? Can we please have an updated GoogleAnalyticsWidget.ascx.cs?
When Judy tells me to be wary of a code I listen, so Katie, your "next release" will only update Able Gold correct? Can we please have an updated GoogleAnalyticsWidget.ascx.cs?
- ImmortalLogic
- Lieutenant (LT)
- Posts: 61
- Joined: Tue May 11, 2010 9:29 am
Re: Google Analytics Upgrade
If you could help us correct that code, we would appreciate it. We just needed something to work immediately and went straight to Google for assistance. We also have that GoogleAnalyticsWidget.ascx.cs widget.
@crazyjoe
The code won't break your code. It just won't pass parameters at some point (possibly). We had added it to the receipt page and didn't call the widget at all anymore.
@crazyjoe
The code won't break your code. It just won't pass parameters at some point (possibly). We had added it to the receipt page and didn't call the widget at all anymore.
Re: Google Analytics Upgrade
Did you ever apply the patch here:I am also looking to do this and I hope there is some updated support for PayPal orders which my Google Analytics still refuses to recognize.
http://help.ablecommerce.com/index.htm# ... paypal.htm
Thank you for choosing AbleCommerce!
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
Re: Google Analytics Upgrade
Hi Katie, I had installed that 4 years ago but for some reason I reverted back. I'll put this up and and see if that does catch my PayPal orders. Thanks
Re: Google Analytics Upgrade
Here is a new user control with updated GA code. Give it a try and provide your feedback.
Re: Google Analytics Upgrade
Thank you SO MUCH Mazher and Katie. I've installed this and we'll see how it worked on Monday.
Re: Google Analytics Upgrade
I am getting an error from this...
[[ConLib:UniversalGoogleAnalyticsWidget]] d:\Domains\XXXXXX\wwwroot\ConLib\UniversalGoogleAnalyticsWidget.ascx.cs(21): error CS0246: The type or namespace name 'StoreSettingsManager' could not be found (are you missing a using directive or an assembly reference?)
Please let me know how to resolve this.
Thanks!
[[ConLib:UniversalGoogleAnalyticsWidget]] d:\Domains\XXXXXX\wwwroot\ConLib\UniversalGoogleAnalyticsWidget.ascx.cs(21): error CS0246: The type or namespace name 'StoreSettingsManager' could not be found (are you missing a using directive or an assembly reference?)
Please let me know how to resolve this.
Thanks!
Re: Google Analytics Upgrade
I made some code and did some creative copy and pasting from my original GoogleAnalyticsWidget.ascx.cs and now I no longer show any errors. Not sure if this will work though.
Line 37 replaced with line 39 from GoogleAnalyticsWidget.ascx.cs
Line 52 replaced with line 54 from GoogleAnalyticsWidget.ascx.cs
Lines 67 - 71 replaced with lines 69 - 73 from GoogleAnalyticsWidget.ascx.cs
Lines 97 - 119 referenced Decimal and not LSDecimal I replaced with lines 95 - 117 from GoogleAnalyticsWidget.ascx.cs
Line 127 - no reference of AbleContext or Current I replaced with line 122 this part Token.Instance.Store.Name.Replace from from GoogleAnalyticsWidget.ascx.cs
Line 182 - 196 referenced Decimal and not LSDecimal I replaced with lines 184 - 198 from GoogleAnalyticsWidget.ascx.cs
Please look this over and let me know how I did
Line 37 replaced with line 39 from GoogleAnalyticsWidget.ascx.cs
Line 52 replaced with line 54 from GoogleAnalyticsWidget.ascx.cs
Lines 67 - 71 replaced with lines 69 - 73 from GoogleAnalyticsWidget.ascx.cs
Lines 97 - 119 referenced Decimal and not LSDecimal I replaced with lines 95 - 117 from GoogleAnalyticsWidget.ascx.cs
Line 127 - no reference of AbleContext or Current I replaced with line 122 this part Token.Instance.Store.Name.Replace from from GoogleAnalyticsWidget.ascx.cs
Line 182 - 196 referenced Decimal and not LSDecimal I replaced with lines 184 - 198 from GoogleAnalyticsWidget.ascx.cs
Please look this over and let me know how I did
Re: Google Analytics Upgrade
Hmm won't let me upload the file. Sorry...
Code: Select all
using CommerceBuilder.Catalog;
using CommerceBuilder.Common;
using CommerceBuilder.Orders;
using CommerceBuilder.Stores;
using CommerceBuilder.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AbleCommerce.ConLib
{
[Description("Widget for Google Analytics Tracking.")]
public partial class UniversalGoogleAnalyticsWidget : System.Web.UI.UserControl
{
private StoreSettingCollection _Settings;
private StringBuilder _GoogleAnalyticsJs;
private bool IsReceiptPage()
{
return (Request.FilePath.IndexOf("Checkout/Receipt.aspx", StringComparison.InvariantCultureIgnoreCase) >= 0);
}
private bool IsMyOrderPage()
{
return (Request.FilePath.IndexOf("Members/MyOrder.aspx", StringComparison.InvariantCultureIgnoreCase) >= 0);
}
protected void Page_PreRender(object sender, EventArgs e)
{
// DETERMINE IF GA IS ENABLED AND CONFIGURED
_Settings = Token.Instance.Store.Settings;
if (_Settings.EnableGoogleAnalyticsPageTracking && !string.IsNullOrEmpty(_Settings.GoogleUrchinId))
{
// CHECK TO SEE IF WE'VE ALREADY REGISTERED ANALYTICS SCRIPT
// THIS IS A SAFETY PRECAUTION IN CASE THE CONTROL GETS ADDED
// TO THE PAGE MORE THAN ONCE
if (!Context.Items.Contains("GoogleAnalytics"))
{
// SHOW THE PANEL AND COMPILE THE TRACKING SCRIPT
GA.Visible = true;
BeginPageTrackerJs();
// SEE IF WE NEED TO ADD THE TRANSACCTION SCRIPT FOR RECEIPT PAGE
if (_Settings.EnableGoogleAnalyticsEcommerceTracking && (this.IsReceiptPage() || this.IsMyOrderPage()))
{
Order order = OrderHelper.GetOrderFromContext();
if (order != null && order.Items.Count > 0)
{
// TRY TO LIMIT REFRESHES FROM MESSING UP THE TRACKING DATA
if (order.OrderDate.AddMinutes(20) > LocaleHelper.LocalNow)
{
// KEEP A LIST OF THE ORDERS WE PROCESS
List<int> googleOrders = Context.Cache.Get("GoogleAnalyticsOrderList") as List<int>;
if (googleOrders == null)
{
googleOrders = new List<int>();
Cache.Add("GoogleAnalyticsOrderList", googleOrders, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 1, 0), CacheItemPriority.Normal, null);
}
// IF WE DON'T HAVE THE ORDER IN OUR LIST, OUTPUT ANALYTICS
if (!googleOrders.Contains(order.OrderId))
{
AddEcommerceJs(order);
googleOrders.Add(order.OrderId);
}
}
}
}
// FINALIZE GA SCRIPT, REGISTER, AND PREVENT REPEAT PROCESSING
ClosePageTrackerJs();
this.Page.Header.Controls.Add(new LiteralControl(_GoogleAnalyticsJs.ToString()));
this.Context.Items.Add("GoogleAnalytics", true);
}
}
}
private void BeginPageTrackerJs()
{
_GoogleAnalyticsJs = new StringBuilder();
_GoogleAnalyticsJs.AppendLine("<script type=\"text/javascript\">");
_GoogleAnalyticsJs.AppendLine("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
_GoogleAnalyticsJs.AppendLine("(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
_GoogleAnalyticsJs.AppendLine("m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
_GoogleAnalyticsJs.AppendLine("})(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
_GoogleAnalyticsJs.AppendLine(string.Format("ga('create', '{0}', 'auto');", _Settings.GoogleUrchinId));
_GoogleAnalyticsJs.AppendLine("ga('send', 'pageview');");
}
private void AddEcommerceJs(Order order)
{
// CALCULATE THE TRANSACTION TOTALS
LSDecimal shippingTotal = 0;
LSDecimal taxTotal = 0;
LSDecimal orderTotal = 0;
foreach (OrderItem item in order.Items)
{
LSDecimal extendedPrice = item.ExtendedPrice;
switch (item.OrderItemType)
{
case OrderItemType.Shipping:
case OrderItemType.Handling:
shippingTotal += extendedPrice;
break;
case OrderItemType.Tax:
taxTotal += extendedPrice;
break;
default:
break;
}
orderTotal += extendedPrice;
}
// LOAD ECOMMERCE PLUGIN
_GoogleAnalyticsJs.AppendLine("ga('require', 'ecommerce', 'ecommerce.js');");
// REGISTER THE TRANSACTION TOTALS
_GoogleAnalyticsJs.AppendLine("ga('ecommerce:addTransaction', {");
_GoogleAnalyticsJs.AppendLine(string.Format("'id': '{0}',", order.OrderNumber));
_GoogleAnalyticsJs.AppendLine(string.Format("'affiliation': '{0}',", Token.Instance.Store.Name.Replace("'", string.Empty)));
_GoogleAnalyticsJs.AppendLine(string.Format("'revenue': '{0:F2}',", orderTotal));
_GoogleAnalyticsJs.AppendLine(string.Format("'tax': '{0:F2}',", taxTotal));
_GoogleAnalyticsJs.AppendLine(string.Format("'shipping': '{0:F2}'", shippingTotal));
_GoogleAnalyticsJs.AppendLine("});");
// REGISTER EACH ITEM
foreach (OrderItem item in order.Items)
{
if (item.OrderItemType == OrderItemType.Product)
{
// REGISTER THIS ITEM
_GoogleAnalyticsJs.Append("ga('ecommerce:addItem', {");
_GoogleAnalyticsJs.AppendLine(string.Format("'id': '{0}',", order.OrderNumber));
_GoogleAnalyticsJs.AppendLine(string.Format("'name': '{0}',", GetItemName(item)));
_GoogleAnalyticsJs.AppendLine(string.Format("'sku': '{0}',", GetItemSku(item)));
_GoogleAnalyticsJs.AppendLine(string.Format("'category': '{0}',", GetCategoryName(item)));
_GoogleAnalyticsJs.AppendLine(string.Format("'price': '{0:F2}',", GetItemPrice(order, item)));
_GoogleAnalyticsJs.AppendLine(string.Format("'quantity': '{0}'", item.Quantity));
_GoogleAnalyticsJs.AppendLine("});");
}
}
// COMMIT THE TRANSACTION
_GoogleAnalyticsJs.Append("ga('ecommerce:send');");
}
private void ClosePageTrackerJs()
{
_GoogleAnalyticsJs.AppendLine("</script>");
}
private string GetItemSku(OrderItem item)
{
if (string.IsNullOrEmpty(item.Sku)) return item.ProductId.ToString();
return SanitizeInput(item.Sku);
}
private string GetItemName(OrderItem item)
{
return SanitizeInput(item.Name);
}
private string GetCategoryName(OrderItem item)
{
if (item.Product != null && item.Product.Categories.Count > 0)
{
int categoryId = item.Product.Categories[0];
Category category = CategoryDataSource.Load(categoryId);
if (category != null) return SanitizeInput(category.Name);
}
return string.Empty;
}
private LSDecimal GetItemPrice(Order order, OrderItem parentItem)
{
OrderItemType[] includedChildTypes = { OrderItemType.Charge, OrderItemType.Coupon, OrderItemType.Credit, OrderItemType.Discount };
LSDecimal totalPrice = parentItem.ExtendedPrice;
foreach (OrderItem childItem in order.Items)
{
if (childItem.ParentItemId == parentItem.OrderItemId
&& childItem.OrderItemId != parentItem.OrderItemId
&& Array.IndexOf(includedChildTypes, childItem.OrderItemType) > -1)
{
totalPrice += childItem.ExtendedPrice;
}
}
return ((decimal)totalPrice / (decimal)parentItem.Quantity);
}
private string SanitizeInput(string value)
{
return value.Replace("'", string.Empty);
}
}
}
Re: Google Analytics Upgrade
I'm guessing this code will only work with Gold installs. When Mazhar gets back from his vacation, I'll ask him look over the code and see if anything can be changed to make it work for 7.0.7.
Thank you for choosing AbleCommerce!
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
Re: Google Analytics Upgrade
I forgot that we were talking about AC7 here not gold. The control I posted earlier is for Gold. Here is the one for AC7
Re: Google Analytics Upgrade
Perfect! Thanks Mazhar and Katie
Re: Google Analytics Upgrade
Joe, have you found anything interesting to sharecrazyjoe wrote:Perfect! Thanks Mazhar and Katie
Re: Google Analytics Upgrade
Thanks for following up Mazher! I am missing about 10% of my orders in Google Analytics. The orders missed are not PayPal orders so i'm not exactly sure where the disconnect is. I will be able to look more in depth tomorrow to try to pinpoint what is missing. My demographics details are filling in nicely so the new Universal Analytics is working.
Re: Google Analytics Upgrade
OK great. Please do update the thread with your findings.
Re: Google Analytics Upgrade
Did you ever find something in common with the orders that are missing? We'd like to go ahead and officially post this as a patch, but are hesitant since you indicated there may be a problem.crazyjoe wrote:Thanks for following up Mazher! I am missing about 10% of my orders in Google Analytics. The orders missed are not PayPal orders so i'm not exactly sure where the disconnect is. I will be able to look more in depth tomorrow to try to pinpoint what is missing. My demographics details are filling in nicely so the new Universal Analytics is working.
Thanks!
Thank you for choosing AbleCommerce!
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support
http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support