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

adds ContentGroup support #15

Merged
merged 1 commit into from
Jul 20, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static bool IsParseableDatatype(this PropertyInfo property)
{
return property.PropertyType == typeof(IList<CustomDimenison>) ||
property.PropertyType == typeof(IList<CustomMetric>) ||
property.PropertyType == typeof(IList<ContentGroup>) ||
property.PropertyType == typeof(string) ||
property.PropertyType == typeof(int) ||
property.PropertyType == typeof(long) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.4-beta</Version>
<Version>1.0.5-beta</Version>
<PackageId>Daimto.Google.Analytics.Tracker.SDK</PackageId>
<Authors>Linda Lawton</Authors>
<Company>Daimto</Company>
Expand All @@ -21,7 +21,7 @@ Supported Platforms:
<PackageProjectUrl>https://github.com/LindaLawton/google-analytics-dotnet-sdk</PackageProjectUrl>
<RepositoryUrl>https://github.com/LindaLawton/google-analytics-dotnet-sdk</RepositoryUrl>
<PackageTags>Google analytics tracker measurement protocol</PackageTags>
<PackageReleaseNotes>adds isvalid for debug response.</PackageReleaseNotes>
<PackageReleaseNotes>adds support for ContentGroup</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/LindaLawton/google-analytics-dotnet-sdk/master/images/analyticsicon.png</PackageIconUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Google.Analytics.SDK.Core.Hits.CustomProperties
{
public class ContentGroupParmBase
{
public string Value { get; set; }
public int Number { get; set; }
public ContentGroupParmBase(int number)
{
if (number < 1 || number > 5) throw new ArgumentOutOfRangeException(nameof(number), "Number must be between 1 - 5");
Number = number;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ public CustomDimenison(int number, string value) : base(number)

public string Value { get; set; }
}


public class ContentGroup : ContentGroupParmBase
{
public ContentGroup(int number, string value) : base(number)
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentOutOfRangeException(nameof(value), "Value must be set.");
Value = value;
}
}
}
28 changes: 23 additions & 5 deletions src/Google.Analytics.SDK.Core/Hits/HitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,16 @@ public abstract class HitBase : IHit
/// You can have up to 5 content groupings, each of which has an associated index between 1 and 5, inclusive. Each content grouping can have up to 100 content groups. The value of a content group is hierarchical text delimited by '/". All leading and trailing slashes will be removed and any repeated slashes will be reduced to a single slash. For example, '/a//b/' will be converted to 'a/b'.
/// </summary>
[Hit(Parm = "cg<groupIndex>", Required = false)]
public Dictionary<int, string> ContentGroup { get; set; } // TODO support like custom dimensions.
public IList<ContentGroup> ContentGroup { get; set; }

public void AddContentGroup(int id, string value)
{
if (ContentGroup == null) ContentGroup = new List<ContentGroup>();

if (ContentGroup.FirstOrDefault(d => id.Equals(d.Number)) != null)
throw new ArgumentOutOfRangeException(nameof(id), "${id} already exists");
ContentGroup.Add(new ContentGroup(id, value));
}

/// <summary>
/// The ID of a clicked DOM element, used to disambiguate multiple links to the same URL in In-Page Analytics reports when Enhanced Link Attribution is enabled for the property.
Expand Down Expand Up @@ -603,12 +612,10 @@ protected virtual bool InternalValidate()

public Dictionary<string, string> GetRequestParamaters()
{

var parms = new Dictionary<string,string>();

try
{

var properties = typeof(HitBase).GetProperties();
foreach (var property in properties)
{
Expand Down Expand Up @@ -640,11 +647,20 @@ public Dictionary<string, string> GetRequestParamaters()
continue;
}

if (property.PropertyType == typeof(IList<ContentGroup>))
{
foreach (var custom in (List<ContentGroup>)value)
{
parms.Add($"cg{custom.Number}", custom.Value);
}
continue;
}

var defalutString = this.BuildPropertyString(name);
var defaultKeyPair = this.GetPropertyString(name);
if (defaultKeyPair.Key != null )
if (defaultKeyPair.Key != null)
parms.Add(defaultKeyPair.Key, defaultKeyPair.Value);

if (string.IsNullOrWhiteSpace(defalutString)) continue;
}

Expand All @@ -658,4 +674,6 @@ public Dictionary<string, string> GetRequestParamaters()
}

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using Google.Analytics.SDK.Core;
using Google.Analytics.SDK.Core.Extensions;
using Google.Analytics.SDK.Core.Hits;
using Google.Analytics.SDK.Core.Hits.WebHits;
using Google.Analytics.SDK.Core.Services.Interfaces;
using Xunit;

namespace Google.Analytics.SDK.Tests.HitTests
{
public class ContentGroupDimensionTests
{
private const string DocumentLocationUrl = "https://plus.google.com/u/0/+LindaLawton/posts/7oxAdszKB9C";
private const int CustomPropertyNumber = 1;
private const int CustomPropertyNumberInvalid = 400;
private const string CustomPropertyValue = "hello";
private const string CustomPropertyNullValue = null;
private const string WebPropertyId = "UA-1111-1";
private const string GACustomPropertyName = "cg";

public HitBase MockHit()
{
return new PageViewHit(DocumentLocationUrl);
}

public GaTracker MockTracker()
{
return TrackerBuilder.BuildWebTracker(WebPropertyId);
}

public string MockCustomProperty()
{
return $"{GACustomPropertyName}{CustomPropertyNumber}={CustomPropertyValue}";
}

[Fact]
public void CreateHitRequest_with_CustomProperty()
{
var tracker = MockTracker();
var hit = MockHit();
hit.AddContentGroup(CustomPropertyNumber, CustomPropertyValue);
var request = (HitRequestBase)tracker.CreateHitRequest(hit);
Assert.Contains(MockCustomProperty(), request.QueryString, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public void Hit_Add_CustomProperty_Test_Property_String_Build()
{
var hit = MockHit();
hit.AddContentGroup(CustomPropertyNumber, CustomPropertyValue);
Assert.Equal(hit.ContentGroup?.FirstOrDefault(d => CustomPropertyNumber.Equals(d.Number) && CustomPropertyValue.Equals(d.Value))?.Number, CustomPropertyNumber);
}

[Fact]
public void Hit_Add_CustomProperty_Duplicate_Number_Fail()
{
var hit = MockHit();
hit.AddContentGroup(CustomPropertyNumber, CustomPropertyValue);
Assert.Throws<ArgumentOutOfRangeException>(() => hit.AddContentGroup(CustomPropertyNumber, CustomPropertyValue));
}

[Fact]
public void Hit_Add_CustomProperty_NullValue_Fail()
{
var hit = MockHit();
Assert.Throws<ArgumentOutOfRangeException>(() => hit.AddContentGroup(CustomPropertyNumber, CustomPropertyNullValue));
}

[Fact]
public void Hit_Add_CustomProperty_ToBigNumber_Fail()
{
var hit = MockHit();
Assert.Throws<ArgumentOutOfRangeException>(() => hit.AddContentGroup(CustomPropertyNumberInvalid, CustomPropertyValue));
}
}
}