Skip to content

Commit 09e9549

Browse files
authored
wrap up for ga (#66)
* add deprecate message to compat classes * update readme * add more examples * changes according to documentation * update table of content * update readme * update unit tests
1 parent 66c0591 commit 09e9549

File tree

11 files changed

+479
-107
lines changed

11 files changed

+479
-107
lines changed

Analytics-CSharp/Segment/Analytics/Analytics.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using global::System.Runtime.Serialization;
55
using global::System.Threading.Tasks;
66
using Segment.Analytics.Plugins;
7+
using Segment.Analytics.Policies;
78
using Segment.Analytics.Utilities;
89
using Segment.Concurrent;
910
using Segment.Serialization;
@@ -300,5 +301,39 @@ public void PurgeStorage(string filePath)
300301
}
301302

302303
#endregion
304+
305+
#region Flush Policy
306+
307+
public void RemoveFlushPolicy(params IFlushPolicy[] policies)
308+
{
309+
foreach (IFlushPolicy policy in policies)
310+
{
311+
policy.Unschedule();
312+
Configuration.FlushPolicies.Remove(policy);
313+
}
314+
}
315+
316+
public void ClearFlushPolicies()
317+
{
318+
foreach (IFlushPolicy policy in Configuration.FlushPolicies)
319+
{
320+
policy.Unschedule();
321+
}
322+
Configuration.FlushPolicies.Clear();
323+
}
324+
325+
public void AddFlushPolicy(params IFlushPolicy[] policies)
326+
{
327+
foreach (IFlushPolicy policy in policies)
328+
{
329+
Configuration.FlushPolicies.Add(policy);
330+
if (_enable)
331+
{
332+
policy.Schedule(this);
333+
}
334+
}
335+
}
336+
337+
#endregion
303338
}
304339
}

Analytics-CSharp/Segment/Analytics/Compat/Migration.cs

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,126 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net.Http.Headers;
45
using System.Reflection;
56
using Segment.Analytics;
67
using Segment.Serialization;
78

8-
namespace Segment.Analytics.Compat {
9+
namespace Segment.Analytics.Compat
10+
{
11+
12+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
913
public class Traits : Dictionary<string, object>
1014
{
1115
}
1216

13-
public class Properties : Dictionary<string, object>
17+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
18+
public class Properties : Dictionary<string, object>
1419
{
1520
}
1621

1722
public static class AnalyticsExtensions
1823
{
24+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
1925
public static void Track(this Analytics analytics, string userId, string eventName)
2026
{
21-
analytics.Track(eventName, new JsonObject() { {"userId", userId} });
27+
analytics.Track(eventName, new JsonObject() {{"userId", userId}});
2228
}
23-
public static void Track(this Analytics analytics, string userId, string eventName, Dictionary<string, object> properties)
29+
30+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
31+
public static void Track(this Analytics analytics, string userId, string eventName,
32+
Dictionary<string, object> properties)
2433
{
2534
properties.Add("userId", userId);
26-
analytics.Track(eventName, JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(properties)));
35+
analytics.Track(eventName,
36+
JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(properties)));
2737
}
2838

39+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
2940
public static void Screen(this Analytics analytics, string userId, string eventName)
3041
{
31-
analytics.Screen(eventName, new JsonObject() { {"userId", userId} });
42+
analytics.Screen(eventName, new JsonObject() {{"userId", userId}});
3243
}
33-
public static void Screen(this Analytics analytics, string userId, string eventName, Dictionary<string, object> properties)
44+
45+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
46+
public static void Screen(this Analytics analytics, string userId, string eventName,
47+
Dictionary<string, object> properties)
3448
{
3549
properties.Add("userId", userId);
36-
analytics.Screen(eventName, JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(properties)));
50+
analytics.Screen(eventName,
51+
JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(properties)));
3752
}
3853

54+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
3955
public static void Page(this Analytics analytics, string userId, string eventName)
4056
{
41-
analytics.Page(eventName, new JsonObject() { {"userId", userId} });
57+
analytics.Page(eventName, new JsonObject() {{"userId", userId}});
4258
}
43-
public static void Page(this Analytics analytics, string userId, string eventName, Dictionary<string, object> properties)
59+
60+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
61+
public static void Page(this Analytics analytics, string userId, string eventName,
62+
Dictionary<string, object> properties)
4463
{
4564
properties.Add("userId", userId);
46-
analytics.Page(eventName, JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(properties)));
65+
analytics.Page(eventName,
66+
JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(properties)));
4767
}
4868

49-
public static void Group(this Analytics analytics, string userId, string groupId, Dictionary<string, object> traits)
69+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
70+
public static void Group(this Analytics analytics, string userId, string groupId,
71+
Dictionary<string, object> traits)
5072
{
5173
traits.Add("userId", userId);
52-
analytics.Group(groupId, JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(traits)));
74+
analytics.Group(groupId,
75+
JsonUtility.FromJson<Segment.Serialization.JsonObject>(JsonUtility.ToJson(traits)));
5376
}
5477

78+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
5579
public static void Alias(this Analytics analytics, string previousId, string userId)
5680
{
5781
analytics._userInfo._userId = previousId;
5882
analytics.Alias(userId);
5983
}
60-
}
84+
}
6185

86+
/// <summary>
87+
/// Plugin that patches user id on a per event basis.
88+
/// This plugin helps migration from the old Analytics.NET and Analytics.Xamarin libraries,
89+
/// since Analytics-CSharp does not support passing user id on every track method.
90+
/// </summary>
91+
[Obsolete("This should only be used if migrating from Analytics.NET or Analytics.Xamarin")]
6292
class UserIdPlugin : EventPlugin
63-
{
64-
public override PluginType Type => PluginType.Enrichment;
65-
66-
public override RawEvent Execute(RawEvent incomingEvent)
6793
{
68-
switch (incomingEvent)
94+
public override PluginType Type => PluginType.Enrichment;
95+
96+
public override RawEvent Execute(RawEvent incomingEvent)
6997
{
70-
case TrackEvent e:
71-
PatchUserId(e, e.Properties);
72-
break;
73-
case PageEvent e:
74-
PatchUserId(e, e.Properties);
75-
break;
76-
case ScreenEvent e:
77-
PatchUserId(e, e.Properties);
78-
break;
79-
case GroupEvent e:
80-
PatchUserId(e, e.Traits);
81-
break;
82-
}
98+
switch (incomingEvent)
99+
{
100+
case TrackEvent e:
101+
PatchUserId(e, e.Properties);
102+
break;
103+
case PageEvent e:
104+
PatchUserId(e, e.Properties);
105+
break;
106+
case ScreenEvent e:
107+
PatchUserId(e, e.Properties);
108+
break;
109+
case GroupEvent e:
110+
PatchUserId(e, e.Traits);
111+
break;
112+
}
83113

84-
return incomingEvent;
85-
}
114+
return incomingEvent;
115+
}
86116

87-
private void PatchUserId(RawEvent @event, JsonObject jsonObject)
88-
{
89-
if (jsonObject.ContainsKey("userId"))
117+
private void PatchUserId(RawEvent @event, JsonObject jsonObject)
90118
{
91-
@event.UserId = jsonObject.GetString("userId");
92-
jsonObject.Remove("userId");
119+
if (jsonObject.ContainsKey("userId"))
120+
{
121+
@event.UserId = jsonObject.GetString("userId");
122+
jsonObject.Remove("userId");
123+
}
93124
}
94125
}
95126
}
96-
}

Analytics-CSharp/Segment/Analytics/Configuration.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private set
3737
}
3838
}
3939

40-
public IAnalyticsErrorHandler AnalyticsErrorHandler { get; private set; }
40+
public IAnalyticsErrorHandler AnalyticsErrorHandler { get; set; }
4141

4242
public IStorageProvider StorageProvider { get; }
4343

@@ -96,11 +96,8 @@ public Configuration(string writeKey,
9696
StorageProvider = storageProvider ?? new DefaultStorageProvider();
9797
HttpClientProvider = httpClientProvider ?? new DefaultHTTPClientProvider();
9898
FlushPolicies = flushPolicies == null ? new ConcurrentList<IFlushPolicy>() : new ConcurrentList<IFlushPolicy>(flushPolicies);
99-
if (FlushPolicies.Count == 0)
100-
{
101-
FlushPolicies.Add(new CountFlushPolicy(flushAt));
102-
FlushPolicies.Add(new FrequencyFlushPolicy(flushInterval * 1000L));
103-
}
99+
FlushPolicies.Add(new CountFlushPolicy(flushAt));
100+
FlushPolicies.Add(new FrequencyFlushPolicy(flushInterval * 1000L));
104101
}
105102

106103
public Configuration(string writeKey,

Analytics-CSharp/Segment/Analytics/Utilities/HTTPClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public abstract class HTTPClient
2323

2424
private readonly string _apiKey;
2525

26-
private readonly string _apiHost;
26+
protected readonly string _apiHost;
2727

28-
private readonly string _cdnHost;
28+
protected readonly string _cdnHost;
2929

3030
private readonly WeakReference<Analytics> _reference = new WeakReference<Analytics>(null);
3131

32-
internal Analytics AnalyticsRef
32+
public Analytics AnalyticsRef
3333
{
3434
get
3535
{

0 commit comments

Comments
 (0)