Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transition library to .NET Core (baseline netstandard2.0/netcoreapp2.1)
- Loading branch information
Showing
162 changed files
with
25,483 additions
and
2,357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ | |
obj/ | ||
bin/ | ||
*.suo | ||
*.user | ||
*.user | ||
.idea/* |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<LangVersion>7.3</LangVersion> | ||
<Nullable>disable</Nullable> | ||
|
||
<!-- NuGet --> | ||
<IncludeSymbols>True</IncludeSymbols> | ||
<IncludeSource>True</IncludeSource> | ||
|
||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>GoogleAnalyticsTracker.snk</AssemblyOriginatorKeyFile> | ||
<DelaySign>false</DelaySign> | ||
|
||
<Description>GoogleAnalyticsTracker - A C# library for tracking Google Analytics.</Description> | ||
<Authors>Maarten Balliauw and contributors</Authors> | ||
<Copyright>Maarten Balliauw and contributors</Copyright> | ||
<PackageProjectUrl>https://github.com/maartenba/GoogleAnalyticsTracker</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/maartenba/GoogleAnalyticsTracker</RepositoryUrl> | ||
<RepositoryType>Git</RepositoryType> | ||
<PackageTags>google analytics ga mvc api rest client tracker stats statistics track usage feature telemetry</PackageTags> | ||
<PackageLicenseExpression>MS-PL</PackageLicenseExpression> | ||
|
||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> | ||
|
||
<Product>GoogleAnalyticsTracker</Product> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using GoogleAnalyticsTracker.Core; | ||
using GoogleAnalyticsTracker.Core.Interface; | ||
using GoogleAnalyticsTracker.Core.TrackerParameters; | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace GoogleAnalyticsTracker.AspNet | ||
{ | ||
[PublicAPI] | ||
public class AspNetCoreTracker : TrackerBase | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
|
||
private static IAnalyticsSession SetupAnalyticsSession( | ||
IHttpContextAccessor contextAccessor, | ||
IOptions<GoogleAnalyticsTrackerOptions> optionsAccessor) | ||
{ | ||
var session = new CookieBasedAnalyticsSession(contextAccessor); | ||
optionsAccessor.Value.CustomizeAnalyticsSession?.Invoke(session); | ||
return session; | ||
} | ||
|
||
private static ITrackerEnvironment SetupTrackerEnvironment( | ||
IOptions<GoogleAnalyticsTrackerOptions> optionsAccessor) | ||
{ | ||
var trackerEnvironment = new AspNetCoreTrackerEnvironment(); | ||
optionsAccessor.Value.CustomizeTrackerEnvironment?.Invoke(trackerEnvironment); | ||
return trackerEnvironment; | ||
} | ||
|
||
public AspNetCoreTracker( | ||
[NotNull] IHttpContextAccessor contextAccessor, | ||
IOptions<GoogleAnalyticsTrackerOptions> optionsAccessor) | ||
: base( | ||
optionsAccessor.Value.TrackerId, | ||
SetupAnalyticsSession(contextAccessor, optionsAccessor), | ||
SetupTrackerEnvironment(optionsAccessor)) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
[UsedImplicitly] | ||
public Task TrackEventAsync(Exception ex) | ||
{ | ||
return TrackEventAsync("Errors", ex.Message, ex.HResult); | ||
} | ||
|
||
[UsedImplicitly] | ||
public async Task TrackEventAsync(string category, string action, int value) | ||
{ | ||
// If no HTTP context available, bail out... | ||
if (_contextAccessor.HttpContext == null) return; | ||
|
||
var eventTrackingParameters = new EventTracking | ||
{ | ||
Category = category, | ||
Action = action, | ||
Label = GetRelativeUrl(), | ||
Value = value, | ||
DocumentHostName = _contextAccessor.HttpContext.Request.Host.Value, | ||
UserAgent = _contextAccessor.HttpContext.Request.Headers["User-Agent"], | ||
UserLanguage = _contextAccessor.HttpContext.Request.Headers["Accept-Language"], | ||
DocumentReferrer = _contextAccessor.HttpContext.Request.Headers["Referrer"], | ||
IpOverride = Environment.GetEnvironmentVariable("server.RemoteIpAddress"), | ||
UserId = _contextAccessor.HttpContext.User.Identity.Name | ||
}; | ||
|
||
await TrackAsync(eventTrackingParameters); | ||
} | ||
|
||
[UsedImplicitly] | ||
public Task TrackPageViewAsync() | ||
{ | ||
return TrackPageViewAsync(null); | ||
} | ||
|
||
[UsedImplicitly] | ||
public async Task TrackPageViewAsync([CanBeNull] string customTitle) | ||
{ | ||
// If no HTTP context available, bail out... | ||
if (_contextAccessor.HttpContext == null) return; | ||
|
||
var pageviewTrackingParameters = new PageView | ||
{ | ||
DocumentTitle = customTitle ?? _contextAccessor.HttpContext.Request.Path.ToString(), | ||
DocumentLocationUrl = GetRelativeUrl(), | ||
DocumentHostName = _contextAccessor.HttpContext.Request.Host.Value, | ||
UserAgent = _contextAccessor.HttpContext.Request.Headers["User-Agent"], | ||
UserLanguage = _contextAccessor.HttpContext.Request.Headers["Accept-Language"], | ||
DocumentReferrer = _contextAccessor.HttpContext.Request.Headers["Referrer"], | ||
IpOverride = Environment.GetEnvironmentVariable("server.RemoteIpAddress"), | ||
UserId = _contextAccessor.HttpContext.User.Identity.Name, | ||
}; | ||
|
||
await TrackAsync(pageviewTrackingParameters); | ||
} | ||
|
||
private string GetRelativeUrl() | ||
{ | ||
return string.IsNullOrEmpty(_contextAccessor.HttpContext.Request.QueryString.ToString()) | ||
? _contextAccessor.HttpContext.Request.Path.ToString() | ||
: string.Format("{0}{1}", _contextAccessor.HttpContext.Request.Path, _contextAccessor.HttpContext.Request.QueryString); | ||
} | ||
} | ||
} |
24 changes: 10 additions & 14 deletions
24
...yticsTracker.Owin/OwinTrackEnvironment.cs → ...spNetCore/AspNetCoreTrackerEnvironment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
GoogleAnalyticsTracker.AspNetCore/AspNetCoreTrackerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Threading.Tasks; | ||
using GoogleAnalyticsTracker.Core; | ||
using GoogleAnalyticsTracker.Core.TrackerParameters; | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace GoogleAnalyticsTracker.AspNet | ||
{ | ||
[PublicAPI] | ||
public static class AspNetCoreTrackerExtensions | ||
{ | ||
public static async Task<TrackingResult> TrackUserTimingAsync(this AspNetCoreTracker tracker, HttpContext httpContext, string pageTitle, string pageUrl, string category, string var, long value, string label = null) | ||
{ | ||
var userTimingParameters = new UserTimings | ||
{ | ||
DocumentTitle = pageTitle, | ||
DocumentLocationUrl = pageUrl, | ||
UserAgent = httpContext.Request.Headers[HeaderNames.UserAgent].ToString(), | ||
DocumentHostName = httpContext.Request.Host.Value, | ||
UserLanguage = httpContext.Request.Headers[HeaderNames.AcceptLanguage].ToString().ToLower(), | ||
UserTimingCategory = category, | ||
UserTimingVariable = var, | ||
UserTimingTime = value, | ||
UserTimingLabel = label, | ||
}; | ||
|
||
return await tracker.TrackAsync(userTimingParameters); | ||
} | ||
|
||
public static async Task<TrackingResult> TrackPageViewAsync(this AspNetCoreTracker tracker, HttpContext httpContext, string pageTitle, string pageUrl = null) | ||
{ | ||
if (pageUrl == null) | ||
{ | ||
pageUrl = httpContext.Request.Path; | ||
} | ||
|
||
var pageViewParameters = new PageView | ||
{ | ||
DocumentTitle = pageTitle, | ||
DocumentLocationUrl = pageUrl, | ||
UserAgent = httpContext.Request.Headers[HeaderNames.UserAgent].ToString(), | ||
DocumentHostName = httpContext.Request.Host.Value, | ||
UserLanguage = httpContext.Request.Headers[HeaderNames.AcceptLanguage].ToString().ToLower(), | ||
IpOverride = httpContext.Connection.RemoteIpAddress.ToString() | ||
}; | ||
|
||
return await tracker.TrackAsync(pageViewParameters); | ||
} | ||
} | ||
} |
Oops, something went wrong.