forked from 40fingers/Dnn-Google-Tag-Manager-Connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleTagManagerConnector.cs
More file actions
210 lines (174 loc) · 6.92 KB
/
Copy pathGoogleTagManagerConnector.cs
File metadata and controls
210 lines (174 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DNN.Connectors.GoogleTagManager
{
using System;
using System.Collections.Generic;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Analytics.Config;
using DotNetNuke.Services.Connections;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
/// <summary>
/// Connector to provide configuration for Google Tag Manager support.
/// </summary>
public class GoogleTagManagerConnector : IConnector
{
private const string DefaultDisplayName = "Google Tag Manager";
private string displayName;
/// <inheritdoc/>
public string Name
{
get { return "Core Google Tag Manager Connector"; }
}
/// <inheritdoc/>
public string IconUrl
{
get { return "~/DesktopModules/Connectors/GoogleTagManager/Images/GoogleTagManager_32X32_Standard.png"; }
}
/// <inheritdoc/>
public string PluginFolder
{
get { return "~/DesktopModules/Connectors/GoogleTagManager/"; }
}
/// <inheritdoc/>
public bool IsEngageConnector
{
get
{
return false;
}
}
/// <inheritdoc/>
public ConnectorCategories Type => ConnectorCategories.Analytics;
/// <inheritdoc/>
// As of DNN 9.2.2 you need to support multiple to get access to the Delete Connection functionality
public bool SupportsMultiple => false;
/// <inheritdoc/>
public string DisplayName
{
get => string.IsNullOrEmpty(this.displayName) ? DefaultDisplayName : this.displayName;
set => this.displayName = value;
}
/// <inheritdoc/>
public string Id { get; set; }
/// <inheritdoc/>
public IEnumerable<IConnector> GetConnectors(int portalId)
{
return new List<IConnector> { this };
}
/// <inheritdoc/>
public void DeleteConnector(int portalId)
{
}
/// <inheritdoc/>
public bool HasConfig(int portalId)
{
IDictionary<string, string> config = this.GetConfig(portalId);
return config.ContainsKey("GtmID") && !string.IsNullOrEmpty(config["GtmID"]);
}
/// <inheritdoc/>
public IDictionary<string, string> GetConfig(int portalId)
{
var gtmConfig = AnalyticsConfiguration.GetConfig("GoogleTagManager");
var portalSettings = new PortalSettings(portalId);
// Important, knockout handles empty strings as false and any other string as true
// so we need to pass empty strings when we mean false, however it passes us back the string "false"
// when saving the settings in the SaveConfig method, so we need to handle that case too
var gtmId = string.Empty;
var trackForAdmin = string.Empty;
if (gtmConfig != null)
{
foreach (AnalyticsSetting setting in gtmConfig.Settings)
{
switch (setting.SettingName.ToLower())
{
case "gtmid":
gtmId = setting.SettingValue;
break;
case "trackforadmin":
trackForAdmin = this.HandleCustomBoolean(setting.SettingValue);
break;
}
}
}
var configItems = new Dictionary<string, string>
{
{ "GtmID", gtmId },
{ "TrackAdministrators", trackForAdmin },
{ "isDeactivating", this.HandleCustomBoolean("false") },
};
return configItems;
}
/// <inheritdoc/>
public bool SaveConfig(int portalId, IDictionary<string, string> values, ref bool validated, out string customErrorMessage)
{
// Delete / Deactivation functionality added into SaveConfig because
// As of DNN 9.2.2 you need to support multiple to get access to the Delete Connection functionality
customErrorMessage = string.Empty;
bool isValid;
try
{
var isDeactivating = false;
bool.TryParse(values["isDeactivating"].ToLowerInvariant(), out isDeactivating);
string gtmID;
string trackForAdmin;
isValid = true;
if (isDeactivating)
{
gtmID = null;
trackForAdmin = null;
}
else
{
gtmID = values["GtmID"] != null ? values["GtmID"].ToUpperInvariant().Trim() : string.Empty;
trackForAdmin = values["TrackAdministrators"] != null ? values["TrackAdministrators"].ToLowerInvariant().Trim() : string.Empty;
if (string.IsNullOrEmpty(gtmID))
{
isValid = false;
customErrorMessage = Localization.GetString("TrackingCodeFormat.ErrorMessage", Constants.LocalResourceFile);
}
}
if (isValid)
{
var config = new AnalyticsConfiguration
{
Settings = new AnalyticsSettingCollection(),
};
config.Settings.Add(new AnalyticsSetting
{
SettingName = "GtmId",
SettingValue = gtmID,
});
config.Settings.Add(new AnalyticsSetting
{
SettingName = "TrackForAdmin",
SettingValue = trackForAdmin,
});
AnalyticsConfiguration.SaveConfig("GoogleTagManager", config);
}
return isValid;
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return false;
}
}
/// <summary>
/// Handles custom conversion from "true" => "true"
/// Anything else to "" to support the strange knockout handling of string as booleans.
/// </summary>
/// <param name="value">The string representing a boolean.</param>
/// <returns>The string representing a boolean after the correction.</returns>
private string HandleCustomBoolean(string value)
{
if (value.Trim().Equals("true", StringComparison.OrdinalIgnoreCase))
{
return "true";
}
return string.Empty;
}
}
}