Skip to content

Commit

Permalink
- Added support for Disabling Advertising Personalization parameter
Browse files Browse the repository at this point in the history
- Added parameterless overload for PostAsync and GetAsync from request level
- Fixes and improvements
  • Loading branch information
Ion Sapoval committed Sep 18, 2021
1 parent e3ed9ca commit 589d9ec
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>GoogleMeasurementProtocol</AssemblyName>
<RootNamespace>GoogleMeasurementProtocol</RootNamespace>
<PackageId>GoogleMeasurementProtocol</PackageId>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<Authors>Ion Sapoval</Authors>
<Company>Ion Sapoval</Company>
<Title>Google Measurement Protocol</Title>
Expand All @@ -15,8 +15,9 @@ This library provides support for creating any type of Measurement Protocol requ
<PackageLicenseUrl></PackageLicenseUrl>
<PackageProjectUrl>https://github.com/ion-sapoval/google-measurement-protocol-dotnet</PackageProjectUrl>
<PackageTags>MeasurementProtocol Google GoogleMeasurementProtocol Analytics</PackageTags>
<PackageReleaseNotes>- Added possibility to inject HttpClient
- Async actions are no longer dependent on captured context.</PackageReleaseNotes>
<PackageReleaseNotes>- Added support for Disabling Advertising Personalization parameter
- Added parameterless overload for PostAsync and GetAsync from request level
- Fixes and improvements</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace GoogleMeasurementProtocol.Parameters.General
{
public class DisablingAdvertisingPersonalization : Parameter
{
/// <summary>
/// Use this parameter to mark an event as disabled for advertising personalization,
/// including for events from a property with a setting that otherwise permits ads personalization.
/// For example, if a transaction is marked to disable advertising personalization,
/// it won't be used when populating a remarketing audience for "past purchasers".
/// </summary>
public DisablingAdvertisingPersonalization(bool value) : base(value)
{
}

public override string Name => "npa";

public override Type ValueType => typeof(bool);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void ValidateRequestParams()
{
RequiredParamsValidator.Validate(_requests[i].Parameters);

CompatibilityValidator.Validate(_requests[i].Parameters, _requests[i],_requests[i].HitType);
CompatibilityValidator.Validate(_requests[i]);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public async Task<RequestValidationResponse> PostAsync(ClientId clientId)
return JsonConvert.DeserializeObject<RequestValidationResponse>(response);
}

public async Task<RequestValidationResponse> PostAsync()
{
return await PostAsync((ClientId)null);
}

public async Task<RequestValidationResponse> PostAsync(UserId userId)
{
var response =
Expand All @@ -37,6 +42,11 @@ public async Task<RequestValidationResponse> GetAsync(ClientId clientId)
return JsonConvert.DeserializeObject<RequestValidationResponse>(response);
}

public async Task<RequestValidationResponse> GetAsync()
{
return await GetAsync((ClientId)null);
}

public async Task<RequestValidationResponse> GetAsync(UserId userId)
{
var response =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public interface IDebugRequest
/// If a hit is valid, parserMessage will be an empty array.</returns>
Task<RequestValidationResponse> PostAsync(ClientId clientId);

/// <summary>
/// Makes an async Post request to Google Analytics Hit Validation
/// </summary>
/// <returns>The response root, hitParsingResult, is an array whose length will correspond to the number of hits sent in the original request.
/// Each object in the array will contain the keys valid, hit, and parserMessage.
/// If a hit is invalid, parserMessage will contain an array of objects describing the validation issues.
/// If a hit is valid, parserMessage will be an empty array.</returns>
Task<RequestValidationResponse> PostAsync();

/// <summary>
/// Makes an async Post request to Google Analytics
/// </summary>
Expand All @@ -35,6 +44,15 @@ public interface IDebugRequest
/// If a hit is valid, parserMessage will be an empty array.</returns>
Task<RequestValidationResponse> GetAsync(ClientId clientId);

/// <summary>
/// Makes an async Get request to Google Analytics Hit Validation
/// </summary>
/// <returns>The response root, hitParsingResult, is an array whose length will correspond to the number of hits sent in the original request.
/// Each object in the array will contain the keys valid, hit, and parserMessage.
/// If a hit is invalid, parserMessage will contain an array of objects describing the validation issues.
/// If a hit is valid, parserMessage will be an empty array.</returns>
Task<RequestValidationResponse> GetAsync();

/// <summary>
/// Makes an async Get request to Google Analytics Hit Validation
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public interface IGoogleAnalyticsRequest
/// <param name="clientId">Anonymously identifies a particular user, device, or browser instance</param>
Task PostAsync(ClientId clientId);

/// <summary>
/// Makes an async Post request to Google Analytics
/// </summary>
Task PostAsync();

/// <summary>
/// Makes an async Post request to Google Analytics
/// </summary>
Expand All @@ -30,6 +35,11 @@ public interface IGoogleAnalyticsRequest
/// <param name="clientId">Anonymously identifies a particular user, device, or browser instance</param>
Task GetAsync(ClientId clientId);

/// <summary>
/// Makes an async Get request to Google Analytics
/// </summary>
Task GetAsync();

/// <summary>
/// Makes an async Get request to Google Analytics
/// </summary>
Expand Down
19 changes: 17 additions & 2 deletions src/GoogleMeasurementProtocol_NetStandard/Requests/RequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public virtual async Task PostAsync(ClientId clientId)
await PostAsync(clientId, GoogleEndpointAddresses.Collect);
}

public virtual async Task PostAsync()
{
await PostAsync((ClientId)null);
}

internal async Task<string> PostAsync(UserId userId, string url)
{
CheckAndAddUserId(userId);
Expand All @@ -80,6 +85,11 @@ public virtual async Task GetAsync(ClientId clientId)
await GetAsync(clientId, GoogleEndpointAddresses.Collect);
}

public virtual async Task GetAsync()
{
await GetAsync((ClientId)null);
}

internal async Task<string> GetAsync(UserId userId, string url)
{
CheckAndAddUserId(userId);
Expand All @@ -98,7 +108,7 @@ protected virtual void ValidateRequestParams()
{
RequiredParamsValidator.Validate(Parameters);

CompatibilityValidator.Validate(Parameters, this, HitType);
CompatibilityValidator.Validate(this);
}

internal void CheckAndAddUserId(UserId userId)
Expand All @@ -116,7 +126,12 @@ internal void CheckAndAddUserId(UserId userId)

internal void CheckAndAddClientId(ClientId clientId)
{
if (clientId?.Value == null)
if (clientId == null)
{
return;
}

if (clientId.Value == null)
{
throw new ArgumentNullException(nameof(clientId));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using GoogleMeasurementProtocol.Parameters;
using GoogleMeasurementProtocol.Requests;

namespace GoogleMeasurementProtocol.Validators
{
public static class CompatibilityValidator
{
public static void Validate(List<Parameter> parameters, RequestBase request, string hitType)
public static void Validate(RequestBase request)
{
foreach (var param in parameters)
foreach (var param in request.Parameters)
{
if (param.SupportedHitTypes != null && !param.SupportedHitTypes.Exists(h => h.Equals(hitType)))
if (param.SupportedHitTypes != null && !param.SupportedHitTypes.Exists(h => h.Equals(request.HitType)))
{
throw new ApplicationException($"Parameters of type '{param.GetType().Name}' are not supported by requests of type {request.GetType().Name}");
}
Expand Down
18 changes: 12 additions & 6 deletions src/TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using GoogleMeasurementProtocol;
using GoogleMeasurementProtocol.Parameters.ContentInformation;
using GoogleMeasurementProtocol.Parameters.General;
using GoogleMeasurementProtocol.Parameters.User;
using GoogleMeasurementProtocol.Requests;

Expand All @@ -12,14 +13,16 @@ class Program
{
static void Main(string[] args)
{
var factory = new GoogleAnalyticsRequestFactory("UA-104485591-1", new HttpClient());
var factory = new GoogleAnalyticsRequestFactory("UA-xxxxxx-x", new HttpClient());

var request1 = factory.CreateRequest(HitTypes.PageView);

request1.Parameters.Add(new DocumentHostName("test55.com"));
request1.Parameters.Add(new DocumentPath("/test/testPath6"));
request1.Parameters.Add(new DocumentTitle("test title2"));
request1.Parameters.Add(new UserId(Guid.NewGuid().ToString()));
request1.Parameters.Add(new ClientId(Guid.NewGuid()));
//request1.Parameters.Add(new UserId(Guid.NewGuid().ToString()));
request1.Parameters.Add(new DisablingAdvertisingPersonalization(true));

var request2 = factory.CreateRequest(HitTypes.PageView);

Expand All @@ -28,15 +31,18 @@ static void Main(string[] args)
request2.Parameters.Add(new DocumentTitle("test title5"));
request2.Parameters.Add(new ClientId(Guid.NewGuid().ToString()));

request1.GetAsync(new ClientId(Guid.NewGuid())).Wait();
request1.PostAsync(new UserId(Guid.NewGuid().ToString())).Wait();
request1.GetAsync().Wait();
request1.PostAsync().Wait();

var batchRequest =
//request1.GetAsync(new ClientId(Guid.NewGuid())).Wait();
//request1.PostAsync(new UserId(Guid.NewGuid().ToString())).Wait();

var batchRequest =
factory.CreateBatchRequest(new List<IGoogleAnalyticsRequest> { request1, request2 });

batchRequest.PostAsync().Wait();

var requestValidationResponse = request1.Debug.PostAsync(new UserId("userId")).Result;
var requestValidationResponse = request1.Debug.PostAsync().Result;

Console.Write("Done!");
Console.ReadKey();
Expand Down
4 changes: 2 additions & 2 deletions src/TestConsole/TestConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="GoogleMeasurementProtocol, Version=2.1.0.0, Culture=neutral, PublicKeyToken=6379dfd9a81b378e, processorArchitecture=MSIL">
<HintPath>..\packages\GoogleMeasurementProtocol.2.1.0\lib\netstandard2.0\GoogleMeasurementProtocol.dll</HintPath>
<Reference Include="GoogleMeasurementProtocol, Version=2.2.0.0, Culture=neutral, PublicKeyToken=6379dfd9a81b378e, processorArchitecture=MSIL">
<HintPath>..\packages\GoogleMeasurementProtocol.2.2.0\lib\netstandard2.0\GoogleMeasurementProtocol.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion src/TestConsole/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="GoogleMeasurementProtocol" version="2.1.0" targetFramework="net461" />
<package id="GoogleMeasurementProtocol" version="2.2.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using GoogleMeasurementProtocol.Parameters;
using GoogleMeasurementProtocol.Parameters.Hit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down

0 comments on commit 589d9ec

Please sign in to comment.