Here is my implementation, any suggest would be appreciated. It works the same on http and https.
1 - Create a new user control in modules named WebCommerceAnalyticsControl.ascx
2 - Rename the file WebCommerceAnalyticsControl.ascx with WebCommerceAnalytics.ascx, just to respect namming convention of nop commerce, but don't propagate renaming to the class name
3 - Make WebCommerceAnalyticsControl class derives from BaseNopUserControl like this
public partial class WebCommerceAnalyticsControl : BaseNopUserControl
4 - Open the WebCommerceAnalytics.ascx.cs file and paste this code inside the class block
protected override void Render(HtmlTextWriter output)
{
if (SettingManager.GetSettingValueBoolean("Analytics.GoogleEnabled"))
{
CultureInfo usCulture = new CultureInfo("en-US");
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script type=\"text/javascript\">");
sb.AppendLine("var _gaq = _gaq || [];");
sb.AppendFormat("_gaq.push(['_setAccount', '{0}']);", SettingManager.GetSettingValue("Analytics.GoogleId"));
sb.AppendLine();
sb.AppendLine("_gaq.push(['_trackPageview']);");
sb.AppendFormat("_gaq.push(['_addTrans', '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}']);", CurrentOrder.OrderId, "extensions-hair.com", CurrentOrder.OrderTotal.ToString("0.00", usCulture), CurrentOrder.OrderTax.ToString("0.00", usCulture), CurrentOrder.OrderShippingInclTax.ToString("0.00", usCulture), CurrentOrder.BillingCity, CurrentOrder.BillingStateProvince, CurrentOrder.BillingCountry);
sb.AppendLine();
foreach (var item in CurrentOrder.OrderProductVariants)
{
string categ = "";
try {categ = item.ProductVariant.Product.ProductCategories[0].Category.Name;}catch (Exception){}
sb.AppendFormat("_gaq.push(['_addItem', '{0}', '{1}', '{2}', '{3}', '{4}', '{5}' ]);", item.OrderId, item.ProductVariant.SKU, item.ProductVariant.FullProductName, categ, item.UnitPriceInclTax.ToString("0.00", usCulture), item.Quantity);
sb.AppendLine();
}
sb.AppendLine("_gaq.push(['_trackTrans']);");
sb.AppendLine("(function() { ");
sb.AppendLine("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
sb.AppendLine("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
sb.AppendLine("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
sb.AppendLine("})();");
sb.AppendLine("</script>");
output.Write(sb.ToString());
}
}
Order currentOrder = null;
public Order CurrentOrder
{
get
{
if (currentOrder == null)
{
var orderCollection = NopContext.Current.User.Orders;
if (orderCollection.Count == 0)
{
Response.Redirect(CommonHelper.GetStoreLocation());
}
else
{
currentOrder = orderCollection[0];
}
}
return currentOrder;
}
}
5 - Compile and insert you new user control at the bottom of CheckoutCompleted.ascx
<%@ Register TagPrefix="nopCommerce" TagName="WebCommerceAnalytics" Src="~/Modules/WebCommerceAnalytics.ascx" %>
and then
<nopCommerce:WebCommerceAnalytics ID="ctrlWebCommerceAnalytics" runat="server" />
6 - Ckeck your settings to have
Analytics.GoogleId = !your id from google!
Analytics.GoogleEnabled = true
Hope it helps!
Regards,
Nicolas