Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Google analytics upgrade to GA4 is required #371 (#380)
Krzysztof Pajakfacd7bb4ace1
diff --git a/src/Plugins/Widgets.GoogleAnalytics/Areas/Admin/Views/WidgetsGoogleAnalytics/Configure.cshtml b/src/Plugins/Widgets.GoogleAnalytics/Areas/Admin/Views/WidgetsGoogleAnalytics/Configure.cshtmlindex 9c7833055..36f9a4936 100644--- a/src/Plugins/Widgets.GoogleAnalytics/Areas/Admin/Views/WidgetsGoogleAnalytics/Configure.cshtml+++ b/src/Plugins/Widgets.GoogleAnalytics/Areas/Admin/Views/WidgetsGoogleAnalytics/Configure.cshtml@@ -6,56 +6,23 @@@await Component.InvokeAsync("StoreScope")@{//default values- var googleAnalytics_trackingScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode("<!-- Google code for Analytics tracking -->" + "\r\n" +- "<script type=\"text/javascript\">" + "\r\n" +- "var _gaq = _gaq || [];" + "\r\n" +- "_gaq.push(['_setAccount', '{GOOGLEID}']);" + "\r\n" +- "_gaq.push(['_trackPageview']);" + "\r\n" +- "{ECOMMERCE}" + "\r\n" +- "(function() {" + "\r\n" +- "var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;" + "\r\n" +- "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';" + "\r\n" +- "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);" + "\r\n" +- "})();" + "\r\n" +- "</script>"));- var googleAnalytics_EcommerceScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode("_gaq.push(['_addTrans', '{ORDERID}', '{SITE}', '{TOTAL}', '{TAX}', '{SHIP}', '{CITY}', '{STATEPROVINCE}', '{COUNTRY}']);" + "\r\n" +- "{DETAILS}" + "\r\n" +- "_gaq.push(['_trackTrans']); "));- var googleAnalytics_EcommerceDetailScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode("_gaq.push(['_addItem', '{ORDERID}', '{PRODUCTSKU}', '{PRODUCTNAME}', '{CATEGORYNAME}', '{UNITPRICE}', '{QUANTITY}' ]); "));-- var universalAnalytics_trackingScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode("<!-- Google code for Analytics tracking -->" + "\r\n" +- "<script>" + "\r\n" +- "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" + "\r\n" +- "(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o)," + "\r\n" +- "m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)" + "\r\n" +- "})(window,document,'script','//www.google-analytics.com/analytics.js','ga');" + "\r\n" +- "ga('create', '{GOOGLEID}', 'auto');" + "\r\n" +- "ga('send', 'pageview');" + "\r\n" +- "{ECOMMERCE}" + "\r\n" +- "</script>"));- var universalAnalytics_EcommerceScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode("ga('require', 'ecommerce', 'ecommerce.js');" + "\r\n" +- "ga('ecommerce:addTransaction', {" + "\r\n" +- "'id': '{ORDERID}'," + "\r\n" +- "'affiliation': '{SITE}'," + "\r\n" +- "'revenue': '{TOTAL}'," + "\r\n" +- "'shipping': '{SHIP}'," + "\r\n" +- "'tax': '{TAX}'" + "\r\n" +- "});" + "\r\n" +- "{DETAILS} " + "\r\n" +- "ga('ecommerce:send');"));- var universalAnalytics_EcommerceDetailScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode("ga('ecommerce:addItem', {" + "\r\n" +- "'id': '{ORDERID}'," + "\r\n" +- "'name': '{PRODUCTNAME}'," + "\r\n" +- "'sku': '{PRODUCTSKU}'," + "\r\n" +- "'category': '{CATEGORYNAME}'," + "\r\n" +- "'price': '{UNITPRICE}'," + "\r\n" +- "'quantity': '{QUANTITY}'" + "\r\n" +- "});"));+ var googleAnalytics_trackingScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(@"<!-- Google tag (gtag.js) -->+ <script async src='https://www.googletagmanager.com/gtag/js?id={GOOGLEID}'></script>+ <script>+ window.dataLayer = window.dataLayer || [];+ function gtag(){dataLayer.push(arguments);}+ gtag('js', new Date());+ gtag('config', '{GOOGLEID}');+ {ECOMMERCE}+ </script>+ "));+ var googleAnalytics_EcommerceScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(@"gtag('event', 'purchase', {transaction_id: '{ORDERID}', value: {TOTAL}, tax: {TAX}, shipping: {SHIP}, currency: '{CURRENCY}', city: '{CITY}', state: '{STATEPROVINCE}', country: '{COUNTRY}', items: [{DETAILS}]});"));+ var googleAnalytics_EcommerceDetailScript = Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(@"{ item_id: '{PRODUCTID}', item_name: '{PRODUCTNAME}', item_category: '{CATEGORYNAME}', price: {UNITPRICE}, quantity: {QUANTITY} }, "));+}<script>$(document).ready(function () {- $("#pre-configure-google-analytics").click(preConfigureGoogleAnalytic);- $("#pre-configure-universal-analytics").click(preConfigureUniversalAnalytic);+ $("#pre-configure-google-analytics").click(preConfigureGoogleAnalytic);});function preConfigureGoogleAnalytic() {@@ -64,11 +31,6 @@$('#@Html.IdFor(model => model.EcommerceDetailScript)').val('@googleAnalytics_EcommerceDetailScript');}- function preConfigureUniversalAnalytic() {- $('#@Html.IdFor(model => model.TrackingScript)').val('@universalAnalytics_trackingScript');- $('#@Html.IdFor(model => model.EcommerceScript)').val('@universalAnalytics_EcommerceScript');- $('#@Html.IdFor(model => model.EcommerceDetailScript)').val('@universalAnalytics_EcommerceDetailScript');- }</script><form asp-controller="WidgetsGoogleAnalytics" asp-action="Configure" method="post">@@ -93,11 +55,7 @@<li>Copy the Tracking ID into the 'ID' box below</li><li>Click the 'Save' button below and Google Analytics will be integrated into your store</li></ul>- <p>- If you would like to switch between Google Analytics (used by default) and Universal Analytics, then please use the buttons below:- </p><input type="button" id="pre-configure-google-analytics" class="k-button" value="Pre-configure Google Analytics" />- <input type="button" id="pre-configure-universal-analytics" class="k-button" value="Pre-configure Universal Analytics" /></div></div><div class="form-group">diff --git a/src/Plugins/Widgets.GoogleAnalytics/Components/WidgetsGoogleAnalyticsViewComponent.cs b/src/Plugins/Widgets.GoogleAnalytics/Components/WidgetsGoogleAnalyticsViewComponent.csindex 8dd43cbce..0b90c1f26 100644--- a/src/Plugins/Widgets.GoogleAnalytics/Components/WidgetsGoogleAnalyticsViewComponent.cs+++ b/src/Plugins/Widgets.GoogleAnalytics/Components/WidgetsGoogleAnalyticsViewComponent.cs@@ -107,8 +107,8 @@ namespace Widgets.GoogleAnalytics.Componentsvar analyticsEcommerceScript = _googleAnalyticsEcommerceSettings.EcommerceScript + "\n";analyticsEcommerceScript = analyticsEcommerceScript.Replace("{GOOGLEID}", _googleAnalyticsEcommerceSettings.GoogleId);analyticsEcommerceScript = analyticsEcommerceScript.Replace("{ORDERID}", order.Id.ToString());- analyticsEcommerceScript = analyticsEcommerceScript.Replace("{SITE}", _workContext.CurrentHost.Url.Replace("http://", "").Replace("/", ""));analyticsEcommerceScript = analyticsEcommerceScript.Replace("{TOTAL}", order.OrderTotal.ToString("0.00", usCulture));+ analyticsEcommerceScript = analyticsEcommerceScript.Replace("{CURRENCY}", order.CustomerCurrencyCode);analyticsEcommerceScript = analyticsEcommerceScript.Replace("{TAX}", order.OrderTax.ToString("0.00", usCulture));var orderShipping = _googleAnalyticsEcommerceSettings.IncludingTax ? order.OrderShippingInclTax : order.OrderShippingExclTax;analyticsEcommerceScript = analyticsEcommerceScript.Replace("{SHIP}", orderShipping.ToString("0.00", usCulture));@@ -135,7 +135,8 @@ namespace Widgets.GoogleAnalytics.Components}analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{ORDERID}", order.Id.ToString());//The SKU code is a required parameter for every item that is added to the transaction- analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{PRODUCTSKU}", FixIllegalJavaScriptChars(product.FormatSku(item.Attributes)));+ analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{PRODUCTSKU}", FixIllegalJavaScriptChars(item.Sku));+ analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{PRODUCTID}", FixIllegalJavaScriptChars(item.ProductId));analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{PRODUCTNAME}", FixIllegalJavaScriptChars(product.Name));analyticsEcommerceDetailScript = analyticsEcommerceDetailScript.Replace("{CATEGORYNAME}", FixIllegalJavaScriptChars(category));var unitPrice = _googleAnalyticsEcommerceSettings.IncludingTax ? item.UnitPriceInclTax : item.UnitPriceExclTax;diff --git a/src/Plugins/Widgets.GoogleAnalytics/GoogleAnalyticPlugin.cs b/src/Plugins/Widgets.GoogleAnalytics/GoogleAnalyticPlugin.csindex 075bd6c18..7a0abb639 100644--- a/src/Plugins/Widgets.GoogleAnalytics/GoogleAnalyticPlugin.cs+++ b/src/Plugins/Widgets.GoogleAnalytics/GoogleAnalyticPlugin.cs@@ -48,23 +48,19 @@ namespace Widgets.GoogleAnalytics{var settings = new GoogleAnalyticsEcommerceSettings{- GoogleId = "UA-0000000-0",- TrackingScript = @"<!-- Google code for Analytics tracking -->+ GoogleId = "000000000",+ TrackingScript = @"<!-- Google tag (gtag.js) -->+ <script async src='https://www.googletagmanager.com/gtag/js?id={GOOGLEID}'></script><script>- var _gaq = _gaq || [];- _gaq.push(['_setAccount', '{GOOGLEID}']);- _gaq.push(['_trackPageview']);- {ECOMMERCE}- (function() {- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);- })();- </script>",- EcommerceScript = @"_gaq.push(['_addTrans', '{ORDERID}', '{SITE}', '{TOTAL}', '{TAX}', '{SHIP}', '{CITY}', '{STATEPROVINCE}', '{COUNTRY}']);- {DETAILS}- _gaq.push(['_trackTrans']); ",- EcommerceDetailScript = @"_gaq.push(['_addItem', '{ORDERID}', '{PRODUCTSKU}', '{PRODUCTNAME}', '{CATEGORYNAME}', '{UNITPRICE}', '{QUANTITY}' ]); ",+ window.dataLayer = window.dataLayer || [];+ function gtag(){dataLayer.push(arguments);}+ gtag('js', new Date());+ gtag('config', '{GOOGLEID}');+ {ECOMMERCE}+ </script>+ ",+ EcommerceScript = @"gtag('event', 'purchase', {transaction_id: '{ORDERID}', value: {TOTAL}, tax: {TAX}, shipping: {SHIP}, currency: '{CURRENCY}', city: '{CITY}', state: '{STATEPROVINCE}', country: '{COUNTRY}', items: [{DETAILS}]});",+ EcommerceDetailScript = @"{ item_id: '{PRODUCTID}', item_name: '{PRODUCTNAME}', item_category: '{CATEGORYNAME}', price: {UNITPRICE}, quantity: {QUANTITY} }, ",ConsentName = "Google Analytics",ConsentDescription = "Allows us to analyse the statistics of visits to our website."};diff --git a/src/Plugins/Widgets.GoogleAnalytics/Manifest.cs b/src/Plugins/Widgets.GoogleAnalytics/Manifest.csindex c9ba3d2c1..473e0f678 100644--- a/src/Plugins/Widgets.GoogleAnalytics/Manifest.cs+++ b/src/Plugins/Widgets.GoogleAnalytics/Manifest.cs@@ -8,5 +8,5 @@ using Widgets.GoogleAnalytics;SystemName = GoogleAnalyticDefaults.ProviderSystemName,SupportedVersion = GrandVersion.SupportedPluginVersion,Author = "grandnode team",- Version = "1.00"+ Version = "1.01")]\ No newline at end of file