-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathAnalyticsServiceExtensions.cs
104 lines (97 loc) · 4.11 KB
/
AnalyticsServiceExtensions.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Parse.Abstractions.Infrastructure;
namespace Parse;
/// <summary>
/// Provides an interface to Parse's logging and analytics backend.
///
/// Methods will return immediately and cache requests (along with timestamps)
/// to be handled in the background.
/// </summary>
public static class AnalyticsServiceExtensions
{
/// <summary>
/// Tracks this application being launched.
/// </summary>
/// <returns>An Async Task that can be waited on or ignored.</returns>
public static Task TrackLaunchAsync(this IServiceHub serviceHub)
{
return TrackLaunchWithPushHashAsync(serviceHub);
}
/// <summary>
/// Tracks the occurrence of a custom event with additional dimensions.
/// Parse will store a data point at the time of invocation with the
/// given event name.
///
/// Dimensions will allow segmentation of the occurrences of this
/// custom event.
///
/// To track a user signup along with additional metadata, consider the
/// following:
/// <code>
/// IDictionary<string, string> dims = new Dictionary<string, string> {
/// { "gender", "m" },
/// { "source", "web" },
/// { "dayType", "weekend" }
/// };
/// ParseAnalytics.TrackEventAsync("signup", dims);
/// </code>
///
/// There is a default limit of 8 dimensions per event tracked.
/// </summary>
/// <param name="name">The name of the custom event to report to ParseClient
/// as having happened.</param>
/// <returns>An Async Task that can be waited on or ignored.</returns>
public static Task TrackAnalyticsEventAsync(this IServiceHub serviceHub, string name)
{
return TrackAnalyticsEventAsync(serviceHub, name, default);
}
/// <summary>
/// Tracks the occurrence of a custom event with additional dimensions.
/// Parse will store a data point at the time of invocation with the
/// given event name.
///
/// Dimensions will allow segmentation of the occurrences of this
/// custom event.
///
/// To track a user signup along with additional metadata, consider the
/// following:
/// <code>
/// IDictionary<string, string> dims = new Dictionary<string, string> {
/// { "gender", "m" },
/// { "source", "web" },
/// { "dayType", "weekend" }
/// };
/// ParseAnalytics.TrackEventAsync("signup", dims);
/// </code>
///
/// There is a default limit of 8 dimensions per event tracked.
/// </summary>
/// <param name="name">The name of the custom event to report to ParseClient
/// as having happened.</param>
/// <param name="dimensions">The dictionary of information by which to
/// segment this event.</param>
/// <returns>An Async Task that can be awaited on or ignored.</returns>
public static async Task TrackAnalyticsEventAsync(this IServiceHub serviceHub, string name, IDictionary<string, string> dimensions)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("A name for the custom event must be provided.", nameof(name));
}
var sessionToken = await serviceHub.CurrentUserController.GetCurrentSessionTokenAsync(serviceHub).ConfigureAwait(false);
await serviceHub.AnalyticsController.TrackEventAsync(name, dimensions, sessionToken, serviceHub).ConfigureAwait(false);
}
/// <summary>
/// Private method, used by platform-specific extensions to report an app-open
/// to the server.
/// </summary>
/// <param name="pushHash">An identifying hash for a given push notification,
/// passed down from the server.</param>
/// <returns>An Async Task that can be waited on or ignored.</returns>
static async Task TrackLaunchWithPushHashAsync(this IServiceHub serviceHub, string pushHash = null)
{
var sessionToken = await serviceHub.CurrentUserController.GetCurrentSessionTokenAsync(serviceHub).ConfigureAwait(false);
await serviceHub.AnalyticsController.TrackAppOpenedAsync(pushHash, sessionToken, serviceHub).ConfigureAwait(false);
}
}