Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Custom Dimensions support to SimpleTracker #117

Merged
merged 2 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions GoogleAnalyticsTracker.Core/TrackerParameters/GeneralParameters.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

using GoogleAnalyticsTracker.Core.TrackerParameters.Interface;

namespace GoogleAnalyticsTracker.Core.TrackerParameters
Expand Down Expand Up @@ -324,5 +326,87 @@ public string ProtocolVersion
public string GoogleDisplayAdsId { get; set; }

#endregion


#region Implementation of ICustomDimensionParameters
/// <summary>
/// Any custom dimensions are set here.
/// </summary>

public void SetCustomDimensions(IDictionary<int, string> customDimensions)
{
if (customDimensions == null || customDimensions.Count <= 0) return;
foreach (var dimension in customDimensions)
{
if (dimension.Value == null) continue;
var value = dimension.Value;

//max length of 150 bytes for custom dimensions
if (value.Length > 149) value = value.Substring(0, 149);

if (dimension.Key == 1) CustomDimension1 = value;
else if (dimension.Key == 2) CustomDimension2 = value;
else if (dimension.Key == 3) CustomDimension3 = value;
else if (dimension.Key == 4) CustomDimension4 = value;
else if (dimension.Key == 5) CustomDimension5 = value;
else if (dimension.Key == 6) CustomDimension6 = value;
else if (dimension.Key == 7) CustomDimension7 = value;
else if (dimension.Key == 8) CustomDimension8 = value;
else if (dimension.Key == 9) CustomDimension9 = value;
else if (dimension.Key == 10) CustomDimension10 = value;
else if (dimension.Key == 11) CustomDimension11 = value;
else if (dimension.Key == 12) CustomDimension12 = value;
else if (dimension.Key == 13) CustomDimension13 = value;
else if (dimension.Key == 14) CustomDimension14 = value;
else if (dimension.Key == 15) CustomDimension15 = value;
else if (dimension.Key == 16) CustomDimension16 = value;
else if (dimension.Key == 17) CustomDimension17 = value;
else if (dimension.Key == 18) CustomDimension18 = value;
else if (dimension.Key == 19) CustomDimension19 = value;
else if (dimension.Key == 20) CustomDimension20 = value;
}
}

[Beacon("cd1")]
public string CustomDimension1 { get; set; }
[Beacon("cd2")]
public string CustomDimension2 { get; set; }
[Beacon("cd3")]
public string CustomDimension3 { get; set; }
[Beacon("cd4")]
public string CustomDimension4 { get; set; }
[Beacon("cd5")]
public string CustomDimension5 { get; set; }
[Beacon("cd6")]
public string CustomDimension6 { get; set; }
[Beacon("cd7")]
public string CustomDimension7 { get; set; }
[Beacon("cd8")]
public string CustomDimension8 { get; set; }
[Beacon("cd9")]
public string CustomDimension9 { get; set; }
[Beacon("cd10")]
public string CustomDimension10 { get; set; }
[Beacon("cd11")]
public string CustomDimension11 { get; set; }
[Beacon("cd12")]
public string CustomDimension12 { get; set; }
[Beacon("cd13")]
public string CustomDimension13 { get; set; }
[Beacon("cd14")]
public string CustomDimension14 { get; set; }
[Beacon("cd15")]
public string CustomDimension15 { get; set; }
[Beacon("cd16")]
public string CustomDimension16 { get; set; }
[Beacon("cd17")]
public string CustomDimension17 { get; set; }
[Beacon("cd18")]
public string CustomDimension18 { get; set; }
[Beacon("cd19")]
public string CustomDimension19 { get; set; }
[Beacon("cd20")]
public string CustomDimension20 { get; set; }
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;

namespace GoogleAnalyticsTracker.Core.TrackerParameters.Interface
{
public interface ICustomDimensionParameters
{
string CustomDimension1 { get; set; }

string CustomDimension2 { get; set; }

string CustomDimension3 { get; set; }

string CustomDimension4 { get; set; }

string CustomDimension5 { get; set; }

string CustomDimension6 { get; set; }

string CustomDimension7 { get; set; }

string CustomDimension8 { get; set; }

string CustomDimension9 { get; set; }

string CustomDimension10 { get; set; }

string CustomDimension11 { get; set; }

string CustomDimension12 { get; set; }

string CustomDimension13 { get; set; }

string CustomDimension14 { get; set; }

string CustomDimension15 { get; set; }

string CustomDimension16 { get; set; }

string CustomDimension17 { get; set; }

string CustomDimension18 { get; set; }

string CustomDimension19 { get; set; }

string CustomDimension20 { get; set; }

}
}
3 changes: 3 additions & 0 deletions GoogleAnalyticsTracker.Core/TrackerParameters/PageView.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace GoogleAnalyticsTracker.Core.TrackerParameters
{
public class PageView : GeneralParameters
Expand All @@ -15,5 +17,6 @@ public override HitType HitType
}

#endregion

}
}
6 changes: 5 additions & 1 deletion GoogleAnalyticsTracker.Simple/EventViewTrackerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;

using GoogleAnalyticsTracker.Core;
Expand All @@ -7,17 +8,20 @@ namespace GoogleAnalyticsTracker.Simple
{
public static class EventTrackerExtensions
{
public static async Task<TrackingResult> TrackEventAsync(this SimpleTracker tracker, string category, string action, string label, long value = 1)
public static async Task<TrackingResult> TrackEventAsync(this SimpleTracker tracker, string category, string action, string label, IDictionary<int, string> customDimensions, long value = 1)
{
var eventTrackingParameters = new EventTracking
{
Category = category,
Action = action,
Label = label,
Value = value,
DocumentHostName = tracker.Hostname,
CacheBuster = tracker.AnalyticsSession.GenerateCacheBuster()
};

eventTrackingParameters.SetCustomDimensions(customDimensions);

return await tracker.TrackAsync(eventTrackingParameters);
}
}
Expand Down
8 changes: 7 additions & 1 deletion GoogleAnalyticsTracker.Simple/PageViewTrackerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using GoogleAnalyticsTracker.Core;
using GoogleAnalyticsTracker.Core.TrackerParameters;
using GoogleAnalyticsTracker.Core.TrackerParameters.Interface;

namespace GoogleAnalyticsTracker.Simple
{
public static class PageViewTrackerExtensions
{
public static async Task<TrackingResult> TrackPageViewAsync(this SimpleTracker tracker, string pageTitle, string pageUrl)
public static async Task<TrackingResult> TrackPageViewAsync(this SimpleTracker tracker, string pageTitle, string pageUrl, IDictionary<int,string> customDimensions)
{
var pageViewParameters = new PageView
{
DocumentTitle = pageTitle,
DocumentLocationUrl = pageUrl,
DocumentHostName = tracker.Hostname,
CacheBuster = tracker.AnalyticsSession.GenerateCacheBuster()
};

pageViewParameters.SetCustomDimensions(customDimensions);

return await tracker.TrackAsync(pageViewParameters);
}
}
Expand Down
6 changes: 5 additions & 1 deletion GoogleAnalyticsTracker.Simple/ScreenviewTrackerExtension.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GoogleAnalyticsTracker.Core;
using GoogleAnalyticsTracker.Core.TrackerParameters;
Expand All @@ -7,18 +8,21 @@ namespace GoogleAnalyticsTracker.Simple
public static class ScreenviewTrackerExtension
{
public static async Task<TrackingResult> TrackScreenviewAsync(this SimpleTracker tracker, string appName,
string appId, string appVersion, string appInstallerId, string screenName)
string appId, string appVersion, string appInstallerId, string screenName, IDictionary<int, string> customDimensions)
{
var screenviewParamenters = new ScreenviewTracking
{
ApplicationName = appName,
ApplicationId = appId,
ApplicationVersion = appVersion,
ApplicationInstallerId = appInstallerId,
DocumentHostName = tracker.Hostname,
ScreenName = screenName,
CacheBuster = tracker.AnalyticsSession.GenerateCacheBuster()
};

screenviewParamenters.SetCustomDimensions(customDimensions);

return await tracker.TrackAsync(screenviewParamenters);
}
}
Expand Down
2 changes: 2 additions & 0 deletions GoogleAnalyticsTracker.Simple/SimpleTrackerEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using GoogleAnalyticsTracker.Core.Interface;
using GoogleAnalyticsTracker.Core.TrackerParameters.Interface;

namespace GoogleAnalyticsTracker.Simple
{
Expand Down Expand Up @@ -37,5 +38,6 @@ public SimpleTrackerEnvironment(string hostname, string osPlatform, string osVer
public string OsPlatform { get; set; }
public string OsVersion { get; set; }
public string OsVersionString { get; set; }

}
}