Skip to content

Commit 0c6ac0f

Browse files
GDPR: Google Analytics
1 parent 4becebe commit 0c6ac0f

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/Plugins/SmartStore.GoogleAnalytics/Controllers/WidgetsGoogleAnalyticsController.cs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using SmartStore.Web.Framework.Controllers;
1515
using SmartStore.Web.Framework.Security;
1616
using SmartStore.Web.Framework.Settings;
17+
using SmartStore.Core.Localization;
1718

1819
namespace SmartStore.GoogleAnalytics.Controllers
1920
{
@@ -37,9 +38,13 @@ public WidgetsGoogleAnalyticsController(
3738
_settingService = settingService;
3839
_orderService = orderService;
3940
_categoryService = categoryService;
40-
}
4141

42-
[AdminAuthorize, ChildActionOnly, LoadSetting]
42+
T = NullLocalizer.Instance;
43+
}
44+
45+
public Localizer T { get; set; }
46+
47+
[AdminAuthorize, ChildActionOnly, LoadSetting]
4348
public ActionResult Configure(GoogleAnalyticsSettings settings)
4449
{
4550
var model = new ConfigurationModel();
@@ -84,9 +89,9 @@ public ActionResult PublicInfo(string widgetZone)
8489
var routeData = ((System.Web.UI.Page)this.HttpContext.CurrentHandler).RouteData;
8590

8691
try
87-
{
88-
//Special case, if we are in last step of checkout, we can use order total for conversion value
89-
if (routeData.Values["controller"].ToString().Equals("checkout", StringComparison.InvariantCultureIgnoreCase) &&
92+
{
93+
// Special case, if we are in last step of checkout, we can use order total for conversion value
94+
if (routeData.Values["controller"].ToString().Equals("checkout", StringComparison.InvariantCultureIgnoreCase) &&
9095
routeData.Values["action"].ToString().Equals("completed", StringComparison.InvariantCultureIgnoreCase))
9196
{
9297
var lastOrder = GetLastOrder();
@@ -110,16 +115,40 @@ private Order GetLastOrder()
110115
null, null, null, null, null, null, null, null, 0, 1).FirstOrDefault();
111116
return order;
112117
}
113-
114-
private string GetTrackingScript()
118+
119+
private string GetOptOutCookieScript()
120+
{
121+
var settings = _settingService.LoadSetting<GoogleAnalyticsSettings>(_storeContext.CurrentStore.Id);
122+
var script = @"
123+
var gaProperty = '{GOOGLEID}';
124+
var disableStr = 'ga-disable-' + gaProperty;
125+
if (document.cookie.indexOf(disableStr + '=true') > -1) {
126+
window[disableStr] = true;
127+
}
128+
function gaOptout() {
129+
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
130+
window[disableStr] = true;
131+
alert('{NOTIFICATION}');
132+
}
133+
";
134+
135+
script = script + "\n";
136+
script = script.Replace("{GOOGLEID}", settings.GoogleId);
137+
script = script.Replace("{NOTIFICATION}", T("Plugins.Widgets.GoogleAnalytics.OptOutNotification").JsText.ToHtmlString());
138+
139+
return script;
140+
}
141+
142+
private string GetTrackingScript()
115143
{
116144
var settings = _settingService.LoadSetting<GoogleAnalyticsSettings>(_storeContext.CurrentStore.Id);
117145
var script = "";
118146
script = settings.TrackingScript + "\n";
119147
script = script.Replace("{GOOGLEID}", settings.GoogleId);
120148
script = script.Replace("{ECOMMERCE}", "");
149+
script = script.Replace("{OPTOUTCOOKIE}", GetOptOutCookieScript());
121150

122-
return script;
151+
return script;
123152
}
124153

125154
private string GetEcommerceScript(Order order)
@@ -131,8 +160,9 @@ private string GetEcommerceScript(Order order)
131160

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

135-
if (order != null)
165+
if (order != null)
136166
{
137167
var site = _storeContext.CurrentStore.Url
138168
.EmptyNull()

src/Plugins/SmartStore.GoogleAnalytics/GoogleAnalyticPlugin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ public class GoogleAnalyticPlugin : BasePlugin, IWidget, IConfigurable
1616

1717
private const string TRACKING_SCRIPT = @"<!-- Google code for Analytics tracking -->
1818
<script>
19+
{OPTOUTCOOKIE}
20+
1921
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
2022
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
2123
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
2224
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
2325
2426
ga('create', '{GOOGLEID}', 'auto');
27+
ga('set', 'anonymizeIp', true);
2528
ga('send', 'pageview');
2629
2730
{ECOMMERCE}

src/Plugins/SmartStore.GoogleAnalytics/Localization/resources.de-de.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<li>Klicken Sie den Speichern-Button.</li>
1515
<li>Aktivieren Sie das Google Analytics Widget unter <b>CMS > Widgets</b> und Google Analytics wird in Ihre Webseite integriert.</li>
1616
</ul>
17+
<p>
18+
Um das Opt-Out-Cookie in Ihrer Datenschutzerklärung einzubinden verwenden Sie folgenden Code:
19+
</p>
20+
<pre>&lt;a href=&quot;javascript:gaOptout()&quot;&gt;Google Analytics deaktivieren&lt;/a&gt;</pre>
1721
]]>
1822
</Value>
1923
</LocaleResource>
@@ -41,4 +45,7 @@
4145
<LocaleResource Name="EcommerceDetailScript.Hint">
4246
<Value>Kopieren Sie den von Google erzeugten Tracking-Code in dieses Feld. {ORDERID}, {PRODUCTSKU}, {PRODUCTNAME}, {CATEGORYNAME}, {UNITPRICE} und {QUANTITY} werden automatisch ersetzt.</Value>
4347
</LocaleResource>
48+
<LocaleResource Name="OptOutNotification">
49+
<Value>Das Tracking ist jetzt deaktiviert</Value>
50+
</LocaleResource>
4451
</Language>

src/Plugins/SmartStore.GoogleAnalytics/Localization/resources.en-us.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<li>Click the Save button below.</li>
1515
<li>Activate the Google Analytics widget under <b>CMS > Widgets</b> to integrate Google Analytics into your store.</li>
1616
</ul>
17+
<p>
18+
To include the opt-out cookie in your privacy policy, use the following code:
19+
</p>
20+
<pre>&lt;a href=&quot;javascript:gaOptout()&quot;&gt;Deactivate Google Analytics&lt;/a&gt;</pre>
1721
]]>
1822
</Value>
1923
</LocaleResource>
@@ -41,4 +45,7 @@
4145
<LocaleResource Name="EcommerceDetailScript.Hint">
4246
<Value>Paste the tracking code generated by Google analytics here. {ORDERID}, {PRODUCTSKU}, {PRODUCTNAME}, {CATEGORYNAME}, {UNITPRICE}, {QUANTITY} will be dynamically replaced.</Value>
4347
</LocaleResource>
48+
<LocaleResource Name="OptOutNotification">
49+
<Value>Tracking is now disabled</Value>
50+
</LocaleResource>
4451
</Language>

0 commit comments

Comments
 (0)