-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleAnalyticsConnector.cs
206 lines (154 loc) · 5.79 KB
/
GoogleAnalyticsConnector.cs
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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using DotNetNuke.Services.Analytics.Config;
using DotNetNuke.Services.Connections;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
namespace MikeSmeltzer.Connectors.ConnectorExample
{
public class GoogleAnalyticsConnector : IConnector
{
#region Properties
private const string DefaultDisplayName = "Google Analytics Connector Example";
public string Name
{
get { return "Google Analytics Connector Example"; }
}
private string _displayName;
public string DisplayName
{
get
{
return
string.IsNullOrEmpty(_displayName) ? DefaultDisplayName : _displayName;
}
set { _displayName = value; }
}
public string IconUrl
{
get { return "~/icons/sigma/GoogleAnalytics_32X32_Standard.png"; }
}
public string PluginFolder
{
get { return "~/DesktopModules/MikeSmeltzer/ConnectorExample/"; }
}
public bool IsEngageConnector
{
get
{
return false;
}
}
public string Id { get; set; }
public ConnectorCategories Type => ConnectorCategories.Analytics;
public bool SupportsMultiple => false;
#endregion
#region Public Methods
public IEnumerable<IConnector> GetConnectors(int portalId)
{
return new List<IConnector> { this };
}
public void DeleteConnector(int portalId)
{
}
public bool HasConfig(int portalId)
{
IDictionary<string, string> config = GetConfig(portalId);
return (config.ContainsKey("TrackingID") && !String.IsNullOrEmpty(config["TrackingID"].ToString()));
}
public IDictionary<string, string> GetConfig(int portalId)
{
var analyticsConfig = AnalyticsConfiguration.GetConfig("GoogleAnalytics");
var trackingId = String.Empty;
var urlParameter = String.Empty;
var trackForAdmin = false;
if (analyticsConfig != null)
{
foreach (AnalyticsSetting setting in analyticsConfig.Settings)
{
switch (setting.SettingName.ToLower())
{
case "trackingid":
trackingId = setting.SettingValue;
break;
case "urlparameter":
urlParameter = setting.SettingValue;
break;
case "trackforadmin":
if (!bool.TryParse(setting.SettingValue, out trackForAdmin))
{
trackForAdmin = true;
}
break;
}
}
}
var configItems = new Dictionary<string, string>
{
{ "TrackingID", trackingId },
{ "UrlParameter", urlParameter},
{ "TrackAdministrators", trackForAdmin.ToString()}
};
return configItems;
}
public bool SaveConfig(int portalId, IDictionary<string, string> values, ref bool validated, out string customErrorMessage)
{
customErrorMessage = string.Empty;
bool isValid;
try
{
string trackingID = values["TrackingID"].ToString().Trim();
string urlParameter = values["UrlParameter"].ToString().Trim();
bool trackForAdmin = false;
trackForAdmin = bool.TryParse(values["TrackAdministrators"].ToLowerInvariant(), out trackForAdmin);
if (!String.IsNullOrEmpty(trackingID) && IsValidGoogleAnalyticsCode(trackingID))
{
isValid = true;
var config = new AnalyticsConfiguration
{
Settings = new AnalyticsSettingCollection()
};
var setting = new AnalyticsSetting
{
SettingName = "TrackingId",
SettingValue = trackingID
};
config.Settings.Add(setting);
setting = new AnalyticsSetting
{
SettingName = "UrlParameter",
SettingValue = urlParameter
};
config.Settings.Add(setting);
setting = new AnalyticsSetting
{
SettingName = "TrackForAdmin",
SettingValue = trackForAdmin.ToString().ToLowerInvariant()
};
config.Settings.Add(setting);
AnalyticsConfiguration.SaveConfig("GoogleAnalytics", config);
} else
{
customErrorMessage = Localization.GetString("TrackingCodeFormat.ErrorMessage", Constants.LocalResourceFile);
isValid = false;
}
return isValid;
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return false;
}
}
#endregion
#region Private Methods
private bool IsValidGoogleAnalyticsCode(string trackingCode)
{
bool isValid = false;
isValid = Regex.IsMatch(trackingCode, "^ua-\\d{4,10}(-\\d{1,4})?$");
return isValid;
}
#endregion
}
}