Skip to content

Commit

Permalink
GDPR: Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Herzog committed May 25, 2018
1 parent 4becebe commit 0c6ac0f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Framework.Security;
using SmartStore.Web.Framework.Settings;
using SmartStore.Core.Localization;

namespace SmartStore.GoogleAnalytics.Controllers
{
Expand All @@ -37,9 +38,13 @@ public WidgetsGoogleAnalyticsController(
_settingService = settingService;
_orderService = orderService;
_categoryService = categoryService;
}

[AdminAuthorize, ChildActionOnly, LoadSetting]
T = NullLocalizer.Instance;
}

public Localizer T { get; set; }

[AdminAuthorize, ChildActionOnly, LoadSetting]
public ActionResult Configure(GoogleAnalyticsSettings settings)
{
var model = new ConfigurationModel();
Expand Down Expand Up @@ -84,9 +89,9 @@ public ActionResult PublicInfo(string widgetZone)
var routeData = ((System.Web.UI.Page)this.HttpContext.CurrentHandler).RouteData;

try
{
//Special case, if we are in last step of checkout, we can use order total for conversion value
if (routeData.Values["controller"].ToString().Equals("checkout", StringComparison.InvariantCultureIgnoreCase) &&
{
// Special case, if we are in last step of checkout, we can use order total for conversion value
if (routeData.Values["controller"].ToString().Equals("checkout", StringComparison.InvariantCultureIgnoreCase) &&
routeData.Values["action"].ToString().Equals("completed", StringComparison.InvariantCultureIgnoreCase))
{
var lastOrder = GetLastOrder();
Expand All @@ -110,16 +115,40 @@ private Order GetLastOrder()
null, null, null, null, null, null, null, null, 0, 1).FirstOrDefault();
return order;
}

private string GetTrackingScript()

private string GetOptOutCookieScript()
{
var settings = _settingService.LoadSetting<GoogleAnalyticsSettings>(_storeContext.CurrentStore.Id);
var script = @"
var gaProperty = '{GOOGLEID}';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
alert('{NOTIFICATION}');
}
";

script = script + "\n";
script = script.Replace("{GOOGLEID}", settings.GoogleId);
script = script.Replace("{NOTIFICATION}", T("Plugins.Widgets.GoogleAnalytics.OptOutNotification").JsText.ToHtmlString());

return script;
}

private string GetTrackingScript()
{
var settings = _settingService.LoadSetting<GoogleAnalyticsSettings>(_storeContext.CurrentStore.Id);
var script = "";
script = settings.TrackingScript + "\n";
script = script.Replace("{GOOGLEID}", settings.GoogleId);
script = script.Replace("{ECOMMERCE}", "");
script = script.Replace("{OPTOUTCOOKIE}", GetOptOutCookieScript());

return script;
return script;
}

private string GetEcommerceScript(Order order)
Expand All @@ -131,8 +160,9 @@ private string GetEcommerceScript(Order order)

script = settings.TrackingScript + "\n";
script = script.Replace("{GOOGLEID}", settings.GoogleId);
script = script.Replace("{OPTOUTCOOKIE}", GetOptOutCookieScript());

if (order != null)
if (order != null)
{
var site = _storeContext.CurrentStore.Url
.EmptyNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ public class GoogleAnalyticPlugin : BasePlugin, IWidget, IConfigurable

private const string TRACKING_SCRIPT = @"<!-- Google code for Analytics tracking -->
<script>
{OPTOUTCOOKIE}
(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');
ga('create', '{GOOGLEID}', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
{ECOMMERCE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<li>Klicken Sie den Speichern-Button.</li>
<li>Aktivieren Sie das Google Analytics Widget unter <b>CMS > Widgets</b> und Google Analytics wird in Ihre Webseite integriert.</li>
</ul>
<p>
Um das Opt-Out-Cookie in Ihrer Datenschutzerklärung einzubinden verwenden Sie folgenden Code:
</p>
<pre>&lt;a href=&quot;javascript:gaOptout()&quot;&gt;Google Analytics deaktivieren&lt;/a&gt;</pre>
]]>
</Value>
</LocaleResource>
Expand Down Expand Up @@ -41,4 +45,7 @@
<LocaleResource Name="EcommerceDetailScript.Hint">
<Value>Kopieren Sie den von Google erzeugten Tracking-Code in dieses Feld. {ORDERID}, {PRODUCTSKU}, {PRODUCTNAME}, {CATEGORYNAME}, {UNITPRICE} und {QUANTITY} werden automatisch ersetzt.</Value>
</LocaleResource>
<LocaleResource Name="OptOutNotification">
<Value>Das Tracking ist jetzt deaktiviert</Value>
</LocaleResource>
</Language>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<li>Click the Save button below.</li>
<li>Activate the Google Analytics widget under <b>CMS > Widgets</b> to integrate Google Analytics into your store.</li>
</ul>
<p>
To include the opt-out cookie in your privacy policy, use the following code:
</p>
<pre>&lt;a href=&quot;javascript:gaOptout()&quot;&gt;Deactivate Google Analytics&lt;/a&gt;</pre>
]]>
</Value>
</LocaleResource>
Expand Down Expand Up @@ -41,4 +45,7 @@
<LocaleResource Name="EcommerceDetailScript.Hint">
<Value>Paste the tracking code generated by Google analytics here. {ORDERID}, {PRODUCTSKU}, {PRODUCTNAME}, {CATEGORYNAME}, {UNITPRICE}, {QUANTITY} will be dynamically replaced.</Value>
</LocaleResource>
<LocaleResource Name="OptOutNotification">
<Value>Tracking is now disabled</Value>
</LocaleResource>
</Language>

0 comments on commit 0c6ac0f

Please sign in to comment.