You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With Analytics-CSharp, you can add Segment analytics to your C# based app which includes Unity, Xamarin, .NET. Analytics-CSharp helps you measure your users, product, and business. It unlocks insights into your app's funnel, core business metrics, and whether you have product-market fit. The Analytics-CSharp library is open-source [on GitHub](https://github.com/segmentio/analytics-csharp){:target="_blank"}.
11
11
12
-
> info ""
13
-
> This library is currently in beta and is governed by Segment’s [First Access and Beta terms](https://www.twilio.com/legal/tos){:target="_blank"}.
14
12
15
13
### Supported platforms
16
14
These platforms support Analytics-CSharp:
@@ -49,23 +47,24 @@ To get started with the Analytics-CSharp library:
49
47
// NOTE: to make Analytics stateless/in-memory,
50
48
// add `InMemoryStorageProvider` to the configuration
51
49
var configuration = new Configuration("<YOUR WRITE KEY>",
52
-
flushAt: 1,
53
-
flushInterval: 10);
50
+
flushAt: 20,
51
+
flushInterval: 30);
54
52
var analytics = new Analytics(configuration);
55
53
```
56
54
57
-
These are the options you can apply to configure the client:
58
-
59
-
Option Name | Description
60
-
----------- | -----------
61
-
`writeKey` *required* | This is your Segment write key.
62
-
`apiHost` | The default is set to `api.segment.io/v1`. <br> This sets a default API Host to which Segment sends events.
63
-
`autoAddSegmentDestination` | The default is set to `true`. <br> This automatically adds the Segment Destination plugin. You can set this to `false` if you want to manually add the Segment Destination plugin.
64
-
`defaultSettings` | The default is set to `{}`. <br> The settings object used as fallback in case of network failure.
65
-
`flushAt` | The default is set to `20`. <br> The count of events at which Segment flushes events.
66
-
`flushInterval` | The default is set to `30` seconds. <br> The interval in seconds at which Segment flushes events.
67
-
`exceptionHandler` | Use to set an exception handler to handle errors happened in async methods within the analytics scope.
68
-
`storageProvider` | Use to set how you want your data to be stored. <br> `DefaultStorageProvider` is used by default which stores data to local storage. `InMemoryStorageProvider` is also provided in the library. You can also write your own storage solution by implementing `IStorageProvider` and `IStorage`.
| `writeKey` *required* | This is your Segment write key. |
58
+
| `flushAt` | Default set to `20`. <br> The count of events at which Segment flushes events. |
59
+
| `flushInterval` | Default set to `30` (seconds). <br> The interval in seconds at which Segment flushes events. |
60
+
| `defaultSettings` | Default set to `{}`. <br> The settings object used as fallback in case of network failure. |
61
+
| `autoAddSegmentDestination` | Default set to `true`. <br> This automatically adds the Segment Destination plugin. You can set this to `false` if you want to manually add the Segment Destination plugin. |
62
+
| `apiHost` | Default set to `api.segment.io/v1`. <br> This sets a default API Host to which Segment sends events. |
63
+
| `cdnHost` | Default set to `cdn-settings.segment.com/v1`. <br> This set a default cdnHost to which Segment fetches settings. |
64
+
| `analyticsErrorHandler` | Default set to `null`. <br>This set an error handler to handle errors happened in analytics |
65
+
| `storageProvider` | Default set to `DefaultStorageProvider`. <br>This set how you want your data to be stored. <br> `DefaultStorageProvider` is used by default which stores data to local storage. `InMemoryStorageProvider` is also provided in the library. <br>You can also write your own storage solution by implementing `IStorageProvider` and `IStorage` |
66
+
| `httpClientProvider` | Default set to `DefaultHTTPClientProvider`. <br>This set a http client provider for analytics use to do network activities. The default provider uses System.Net.Http for network activities. |
67
+
| `flushPolicies` | Default set to `null`. <br>This set custom flush policies to tell analytics when and how to flush. By default, it converts `flushAt` and `flushInterval` to `CountFlushPolicy` and `FrequencyFlushPolicy`. If a value is given, it overwrites `flushAt` and `flushInterval`. |
69
68
70
69
## Tracking Methods
71
70
@@ -323,7 +322,213 @@ The `reset` method clears the SDK’s internal stores for the current user and g
323
322
analytics.Reset()
324
323
```
325
324
326
-
## Arrays
325
+
326
+
## Controlling Upload With Flush Policies
327
+
Tomoregranularlycontrol when events are uploaded you can use `FlushPolicies`. **This will override any setting on `flushAt` and `flushInterval`, but you can use `CountFlushPolicy` and `FrequencyFlushPolicy` to have the same behaviour respectively.**
328
+
329
+
AFlushPolicydefinesthestrategyfordeciding when to flush, this can be on an interval, on a certain time of day, after receiving a certain number of events or even after receiving a particular event. This gives you even more flexibility on when to send event to Segment.
OneofthemainadvantagesofFlushPoliciesisthatyoucanaddandremovepoliciesonthefly. Thisisverypowerful when you want to reduce or increase the amount of flushes.
Theerrorhandlerconfigurationrequiresaninstancethatimplements `IAnalyticsErrorHandler` whichwillgetcalledwheneveranerrorhappensontheanalyticsclient. Itwillreceiveageneral `Exception`, butyoucancheckiftheexceptionisatypeof `AnalyticsError` andconvertstogetmoreinfoabouttheerror. Checkout [here](https://github.com/segmentio/Analytics-CSharp/blob/main/Analytics-CSharp/Segment/Analytics/Errors.cs#L77) to see a full list of error types that analytics throws.
Pluginscanalsoreporterrorstothehandlerbyusingthe [`.ReportInternalError`](https://github.com/segmentio/Analytics-CSharp/blob/main/Analytics-CSharp/Segment/Analytics/Errors.cs#L54) function of the analytics client, we recommend using the `AnalyticsErrorType.PluginError` for consistency, and attaching the `exception` with the actual exception that was hit:
440
+
441
+
```csharp
442
+
try
443
+
{
444
+
// do something;
445
+
}
446
+
catch (Exceptione)
447
+
{
448
+
this.Analytics.ReportInternalError(AnalyticsErrorType.PluginError, e, "Error from plugin");
449
+
Analytics.Logger.Log(LogLevel.Error, e);
450
+
}
451
+
```
452
+
453
+
### Listen to Analytics Logs
454
+
455
+
Besideserrorhandling, youcouldalsoprovideastatic `ISegmentLogger` tohelploganddebugaswellaserrorhandling. Thesamelogthatisreportedby `ReportInternalError` isalsoreportedtothisstaticlogger. Thestaticloggeralsoreceivesmoreerrorsandexceptionsbecauseitdoesnotrequirean `Analytics` instanceavailable. Thus, it's also a good idea to use the logger as an addition to `IAnalyticsErrorHandler`.
TheSDKallowsyoutohavefullcontroloverthenetworkcomponents. YoucaneasilyswapoutSystem.Netwithyourfavoritenetworklibrarybyimplementing `IHTTPClientProvider` andextending `HTTPClient`. Takealookat [thisexample](https://github.com/segmentio/Analytics-CSharp/blob/main/Samples/UnitySample/UnityHTTPClient.cs) where the default http client is fully replaced by Unity's `UnityWebRequest`.
Tosendanarrayasaneventproperty, referencethe [GitHubrepo](https://github.com/segmentio/Serialization.NET/blob/main/Tests/JsonUtilityTest.cs#L24){:target="_blank"}. Below is an example of code you can implement to send an array of strings:
328
533
329
534
```c#
@@ -335,8 +540,30 @@ JsonObject customerJsonObj = new JsonObject
0 commit comments