From 5d6d00e8595326203b6dca144fe3c7fa1bdb9120 Mon Sep 17 00:00:00 2001 From: Oliver Hine Date: Tue, 14 May 2019 10:41:09 -0400 Subject: [PATCH 1/5] fix TrackAdmin setting always saving as true. add anonymizeIp and trackUserID settings. --- .../App_LocalResources/SharedResources.resx | 6 +++ .../GoogleAnalyticsConnector.cs | 5 +- .../GoogleAnalytics/Scripts/connector.js | 5 +- .../Connectors/GoogleAnalytics/connector.htm | 10 ++++ .../Analytics/GoogleAnalyticsEngine.cs | 47 +++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/DNN Platform/Connectors/GoogleAnalytics/App_LocalResources/SharedResources.resx b/DNN Platform/Connectors/GoogleAnalytics/App_LocalResources/SharedResources.resx index b7dcc2137ea..2bd85bcd86f 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/App_LocalResources/SharedResources.resx +++ b/DNN Platform/Connectors/GoogleAnalytics/App_LocalResources/SharedResources.resx @@ -135,4 +135,10 @@ Url Parameter: + + Anonymize IP: + + + Track UserID: + \ No newline at end of file diff --git a/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs b/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs index 189a1bc465c..f42e6c914a3 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs +++ b/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs @@ -117,6 +117,8 @@ public IDictionary GetConfig(int portalId) { "TrackingID", trackingId }, { "UrlParameter", urlParameter}, { "TrackAdministrators", trackForAdmin.ToString()}, + { "AnonymizeIp", trackForAdmin.ToString()}, + { "TrackUserId", trackForAdmin.ToString()}, { "isDeactivating", false.ToString()} }; @@ -158,7 +160,8 @@ public bool SaveConfig(int portalId, IDictionary values, ref boo trackingID = values["TrackingID"] != null ? values["TrackingID"].ToString().Trim() : String.Empty; urlParameter = values["UrlParameter"] != null ? values["UrlParameter"].ToString().Trim() : String.Empty; - trackForAdmin = bool.TryParse(values["TrackAdministrators"].ToLowerInvariant(), out trackForAdmin); + + bool.TryParse(values["TrackAdministrators"].ToLowerInvariant(), out trackForAdmin); if (String.IsNullOrEmpty(trackingID)) { diff --git a/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js b/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js index cf5550b53f9..db4902f3bb1 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js +++ b/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js @@ -31,7 +31,8 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip // Convert boolean to string as the API requires a dictionary of string values conn.configurations[2].value(conn.configurations[2].value().toString()); - + conn.configurations[3].value(conn.configurations[3].value().toString()); + conn.configurations[4].value(conn.configurations[4].value().toString()); } var onSaveComplete = function (conn, id) { @@ -105,7 +106,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip // Set the isDeactivating flag to true to override the default save behaviour // Temporary workaround until delete functionality on connectors is improved - conn.configurations[3].value("true"); + conn.configurations[5].value("true"); wasDeactivated = true; conn.save(conn, e, onSaveComplete.bind(this, conn, conn.id)); } diff --git a/DNN Platform/Connectors/GoogleAnalytics/connector.htm b/DNN Platform/Connectors/GoogleAnalytics/connector.htm index dc673867e35..85fdc49357d 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/connector.htm +++ b/DNN Platform/Connectors/GoogleAnalytics/connector.htm @@ -15,6 +15,16 @@ +
+ + +
+ +
+ + +
+
\ No newline at end of file diff --git a/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs b/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs index 33e37955c07..32ddb59bfcb 100644 --- a/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs +++ b/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs @@ -94,5 +94,52 @@ public override string RenderScript(string scriptTemplate) scriptTemplate = scriptTemplate.Replace("[CUSTOM_SCRIPT]", RenderCustomScript(config)); return scriptTemplate; } + + public override string RenderCustomScript(AnalyticsConfiguration config) + { + try + { + bool anonymize = false; + bool trackingUserId = false; + + foreach (AnalyticsSetting setting in config.Settings) + { + switch (setting.SettingName.ToLowerInvariant()) + { + case "anonymizeip": + { + bool.TryParse(setting.SettingValue, out anonymize); + break; + } + case "trackinguser": + { + bool.TryParse(setting.SettingValue, out trackingUserId); + break; + } + } + } + + + var sb = new System.Text.StringBuilder(); + if (anonymize && !trackingUserId) + { + sb.Append("ga('set', 'anonymizeIp', true);"); + } + + if (trackingUserId && !anonymize) + { + sb.AppendFormat("ga('set', 'userId', {0});", UserController.Instance.GetCurrentUserInfo().UserID); + } + + return sb.ToString(); + } + catch (Exception ex) + { + Exceptions.Exceptions.LogException(ex); + + return string.Empty; + } + } + } } \ No newline at end of file From ea8e78cdd84e0427f8921ab4a6ddd625fecd01e3 Mon Sep 17 00:00:00 2001 From: Oliver Hine Date: Tue, 14 May 2019 10:48:50 -0400 Subject: [PATCH 2/5] Allow anonymizeIp and trackingUserId to both be enabled. --- .../Library/Services/Analytics/GoogleAnalyticsEngine.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs b/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs index 32ddb59bfcb..d6faa1d8bb8 100644 --- a/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs +++ b/DNN Platform/Library/Services/Analytics/GoogleAnalyticsEngine.cs @@ -121,12 +121,12 @@ public override string RenderCustomScript(AnalyticsConfiguration config) var sb = new System.Text.StringBuilder(); - if (anonymize && !trackingUserId) + if (anonymize) { sb.Append("ga('set', 'anonymizeIp', true);"); } - if (trackingUserId && !anonymize) + if (trackingUserId) { sb.AppendFormat("ga('set', 'userId', {0});", UserController.Instance.GetCurrentUserInfo().UserID); } From 00916559d800bc29f49ecdcec88f34a1b4bb81a7 Mon Sep 17 00:00:00 2001 From: Oliver Hine Date: Tue, 14 May 2019 19:12:45 -0400 Subject: [PATCH 3/5] fixes issues discovered during code review. --- .../GoogleAnalyticsConnector.cs | 43 +++++++++++++++---- .../Connectors/GoogleAnalytics/connector.htm | 2 +- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs b/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs index f42e6c914a3..1fc9d103244 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs +++ b/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs @@ -88,6 +88,8 @@ public IDictionary GetConfig(int portalId) var trackingId = String.Empty; var urlParameter = String.Empty; var trackForAdmin = false; + var anonymizeIp = false; + var trackUserId = false; if (analyticsConfig != null) { @@ -108,6 +110,18 @@ public IDictionary GetConfig(int portalId) trackForAdmin = true; } break; + case "anonymizeip": + if (!bool.TryParse(setting.SettingValue, out anonymizeIp)) + { + anonymizeIp = false; + } + break; + case "trackuserid": + if (!bool.TryParse(setting.SettingValue, out trackUserId)) + { + trackUserId = false; + } + break; } } } @@ -117,8 +131,8 @@ public IDictionary GetConfig(int portalId) { "TrackingID", trackingId }, { "UrlParameter", urlParameter}, { "TrackAdministrators", trackForAdmin.ToString()}, - { "AnonymizeIp", trackForAdmin.ToString()}, - { "TrackUserId", trackForAdmin.ToString()}, + { "AnonymizeIp", anonymizeIp.ToString()}, + { "TrackUserId", trackUserId.ToString()}, { "isDeactivating", false.ToString()} }; @@ -142,33 +156,32 @@ public bool SaveConfig(int portalId, IDictionary values, ref boo string trackingID; string urlParameter; bool trackForAdmin; + bool anonymizeIp; + bool trackUserId; isValid = true; - if (isDeactivating) { - trackingID = null; urlParameter = null; trackForAdmin = false; - - + anonymizeIp = false; + trackUserId = false; } else { - trackingID = values["TrackingID"] != null ? values["TrackingID"].ToString().Trim() : String.Empty; urlParameter = values["UrlParameter"] != null ? values["UrlParameter"].ToString().Trim() : String.Empty; bool.TryParse(values["TrackAdministrators"].ToLowerInvariant(), out trackForAdmin); + bool.TryParse(values["AnonymizeIp"].ToLowerInvariant(), out anonymizeIp); + bool.TryParse(values["TrackUserId"].ToLowerInvariant(), out trackUserId); if (String.IsNullOrEmpty(trackingID)) { - isValid = false; customErrorMessage = Localization.GetString("TrackingCodeFormat.ErrorMessage", Constants.LocalResourceFile); - } } @@ -204,6 +217,18 @@ public bool SaveConfig(int portalId, IDictionary values, ref boo SettingValue = trackForAdmin.ToString().ToLowerInvariant() }; + setting = new AnalyticsSetting + { + SettingName = "AnonymizeIp", + SettingValue = anonymizeIp.ToString().ToLowerInvariant() + }; + + setting = new AnalyticsSetting + { + SettingName = "TrackUserId", + SettingValue = trackUserId.ToString().ToLowerInvariant() + }; + config.Settings.Add(setting); AnalyticsConfiguration.SaveConfig("GoogleAnalytics", config); diff --git a/DNN Platform/Connectors/GoogleAnalytics/connector.htm b/DNN Platform/Connectors/GoogleAnalytics/connector.htm index 85fdc49357d..69cba741030 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/connector.htm +++ b/DNN Platform/Connectors/GoogleAnalytics/connector.htm @@ -22,7 +22,7 @@
- +
From 58c6fce83e46a7412ef2fe7df3fb2b94e4d66f45 Mon Sep 17 00:00:00 2001 From: Oliver Hine Date: Tue, 14 May 2019 19:15:43 -0400 Subject: [PATCH 4/5] update syntax for SaveConfig --- .../GoogleAnalyticsConnector.cs | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs b/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs index 1fc9d103244..8a677c2a1df 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs +++ b/DNN Platform/Connectors/GoogleAnalytics/GoogleAnalyticsConnector.cs @@ -194,42 +194,35 @@ public bool SaveConfig(int portalId, IDictionary values, ref boo Settings = new AnalyticsSettingCollection() }; - - var setting = new AnalyticsSetting + config.Settings.Add(new AnalyticsSetting { SettingName = "TrackingId", SettingValue = trackingID - }; - - config.Settings.Add(setting); + }); - setting = new AnalyticsSetting + config.Settings.Add(new AnalyticsSetting { SettingName = "UrlParameter", SettingValue = urlParameter - }; - - config.Settings.Add(setting); + }); - setting = new AnalyticsSetting + config.Settings.Add(new AnalyticsSetting { SettingName = "TrackForAdmin", SettingValue = trackForAdmin.ToString().ToLowerInvariant() - }; + }); - setting = new AnalyticsSetting + config.Settings.Add(new AnalyticsSetting { SettingName = "AnonymizeIp", SettingValue = anonymizeIp.ToString().ToLowerInvariant() - }; + }); - setting = new AnalyticsSetting + config.Settings.Add(new AnalyticsSetting { SettingName = "TrackUserId", SettingValue = trackUserId.ToString().ToLowerInvariant() - }; - - config.Settings.Add(setting); + }); AnalyticsConfiguration.SaveConfig("GoogleAnalytics", config); } From 42a509eb69a412c77e94a44366b05b5e1f8025eb Mon Sep 17 00:00:00 2001 From: Oliver Hine Date: Tue, 14 May 2019 19:24:16 -0400 Subject: [PATCH 5/5] fixed ESLint warnings. --- .../GoogleAnalytics/Scripts/connector.js | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js b/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js index db4902f3bb1..c521e5851d6 100644 --- a/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js +++ b/DNN Platform/Connectors/GoogleAnalytics/Scripts/connector.js @@ -25,7 +25,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip window.String.format = !window.String.format ? window.top.String.format : window.String.format; } - } + }; var onSave = function (conn) { @@ -33,7 +33,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip conn.configurations[2].value(conn.configurations[2].value().toString()); conn.configurations[3].value(conn.configurations[3].value().toString()); conn.configurations[4].value(conn.configurations[4].value().toString()); - } + }; var onSaveComplete = function (conn, id) { @@ -52,7 +52,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip conn.connected("true"); - if (bindViewModel.buttons().length == 1) { + if (bindViewModel.buttons().length === 1) { activateDeleteButton(conn); @@ -60,11 +60,11 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip } - } + }; var activateDeleteButton = function (conn) { - if (conn.buttons().length == 1) { + if (conn.buttons().length === 1) { conn.buttons.push({ className: "secondarybtn", @@ -81,7 +81,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip } - } + }; var getActionButtons = function () { @@ -94,9 +94,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip text: utility.resx.Connectors.btn_Save, action: function (conn, e) { conn.save(conn, e, onSaveComplete.bind(this, conn, conn.id)); - }, - - + } }, { @@ -111,7 +109,7 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip conn.save(conn, e, onSaveComplete.bind(this, conn, conn.id)); } } - ] + ]; } else { @@ -121,16 +119,11 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip text: utility.resx.Connectors.btn_Save, action: function (conn, e) { conn.save(conn, e, onSaveComplete.bind(this, conn, conn.id)); - }, - - + } } - ] - - + ]; } - - } + }; return { @@ -138,6 +131,6 @@ define(["jquery", "knockout", "templatePath/scripts/config", "templatePath/scrip onSave: onSave, getActionButtons: getActionButtons - } + }; }); \ No newline at end of file