---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/segment/connections/sources/catalog/libraries/mobile/xamarin/analytics-xamarin#article
headline: Analytics for Xamarin
description: Integrate Segment analytics into your Xamarin app with the Analytics.Xamarin library, including Identify, Track, Screen, Group, and Alias methods.
url: https://www.twilio.com/docs/segment/connections/sources/catalog/libraries/mobile/xamarin/analytics-xamarin
inLanguage: en
dateModified: 2026-06-25T22:35:15.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# Analytics for Xamarin

**Support:**

* Community ✓
* Maintenance ✕
* Flagship ✕

Segment doesn't manage or update community libraries. These libraries are available on GitHub under the MIT License for the open-source community to fork or contribute.

> \[!WARNING]
>
> End-of-support for the Analytics.Xamarin SDK is scheduled for **March 2026**. Segment's future development efforts concentrate on the new [Analytics-CSharp](/docs/segment/connections/sources/catalog/libraries/server/csharp/) SDK. If you'd like to migrate to Analytics-CSharp, see the [migration guide](/docs/segment/connections/sources/catalog/libraries/server/csharp/migration-guide/).

Segment's [Xamarin](http://xamarin.com/) Portable Class Library ([PCL](http://developer.xamarin.com/guides/cross-platform/application_fundamentals/pcl/)) is the best way to integrate analytics into your Xamarin application. It lets you record analytics data from your C#, F#, and .NET code, and supports `PCL Profile 4.0 - Profile136`, which targets the following platforms:

* .NET Framework 4 or later
* Windows Phone 8 or later
* Silverlight 5
* Windows 8
* Windows Phone Silverlight 8
* Windows Store apps (Windows 8)
* Xamarin.Android
* Xamarin.iOS

The library issues requests that hit our servers, and then we route your data to any analytics service you enable on our destinations page. This library is open-source, so you can [check it out on GitHub](https://github.com/segmentio/Analytics.Xamarin).

**Note**: Since Xamarin requires Segment's library to be portable to different builds, Segment can only enable server-side destinations, as opposed to bundling select native SDKs like we do for iOS and Android. Look for the "Server" icon when selecting destinations. For tools for which we offer both bundled and server-side destinations, like Mixpanel, Amplitude, and Google Analytics, Segment's Xamarin library will only be able to use their server-side functionality.

## Getting started

Clone `Analytics.Xamarin` from [GitHub](https://github.com/segmentio/Analytics.Xamarin):

```bash
git clone https://github.com/segmentio/Analytics.Xamarin.git
```

Import the `Analytics.Xamarin` project into Xamarin Studio, and add it as a reference to your code.

Now you'll need to initialize the library.

```csharp
using Segment;

// initialize with your Segment source write key ...
Analytics.Initialize("YOUR_WRITE_KEY");
```

You only need to initialize once at the start of your program. You can then keep using the `Analytics` singleton anywhere in your code.

The default initialization settings are production-ready and queue messages on another thread before sending any requests. In development you might want to use [development settings](/docs/segment/connections/sources/catalog/libraries/server/csharp#development-settings).

## Identify

Identify lets you tie a user to their actions and record traits about them. It includes a unique User ID and any optional traits you know about them.

We recommend calling Identify a single time when the user's account is first created, and only identifying again later when their traits change.

Example Identify call:

```csharp
Analytics.Client.Identify("019mr8mf4r", new Traits() {
    { "name", "Tom Smykowski" },
    { "email", "tom@example.com" },
    { "friends", 29 }
});
```

This example call identifies Tom by his unique User ID (the one you know him by in your database) and label him with `name`, `email` and `friends` traits.

The Identify call has the following fields:

| `userId` *String*             | The ID for this user in your database.                                                                                                                                 |
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Traits` *Traits, optional*   | A dictionary of traits you know about the user. Things like: `email`, `name` or `friends`.                                                                             |
| `options` *Options, optional* | An `Options` object lets you set a [timestamp](#historical-import), [enable or disable destinations](#selecting-destinations), or [send additional context](#context). |

Find details on the **identify method payload** in our [Spec](/docs/segment/connections/spec/identify/).

## Track

Track lets you record the actions your users perform. Every action triggers what we call an "event", which can also have associated properties.

You'll want to track events that are indicators of success for your site, like **Signed Up**, **Item Purchased** or **Article Bookmarked**.

To get started, we recommend tracking just a few important events. You can always add more later!

Example Track call:

```csharp
Analytics.Client.Track("019mr8mf4r", "Item Purchased", new Properties() {
    { "revenue", 39.95 },
    { "shipping", "2-day" }
});
```

This example Track call tells us that your user just triggered the **Item Purchased** event with a revenue of $39.95 and chose your hypothetical '2-day' shipping.

Track event properties can be anything you want to record.

The Track call has the following fields:

| `userId` *String*                   | The ID for this user in your database.                                                                                                                                 |
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `event` *String*                    | The name of the event you're tracking. We recommend human-readable names like **Played Song** or **Updated Status**.                                                   |
| `properties` *Properties, optional* | A dictionary of properties for the event. If the event was **Added to Cart**, it might have properties like `price` or `product`.                                      |
| `options` *Options, optional*       | An `Options` object lets you set a [timestamp](#historical-import), [enable or disable destinations](#selecting-destinations), or [send additional context](#context). |

Find details on **best practices in event naming** as well as the **Track method payload** in our [Spec](/docs/segment/connections/spec/track/).

## Screen

The [Screen](/docs/segment/connections/spec/screen/) method lets you record whenever a user sees a screen of your mobile app, along with optional extra information about the page being viewed.

You'll want to record a screen event whenever the user opens a screen in your app. This could be a view, fragment, dialog or activity depending on your app.

Not all services support screen, so when it's not supported explicitly, the screen method tracks as an event with the same parameters.

Example Screen call:

```csharp
Analytics.Client.Screen("019mr8mf4r", "Register", new Properties() {
    { "type", "facebook" }
});
```

The Screen call has the following fields:

| `userId` *String*                   | The ID for this user in your database.                                                                                                                                 |
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` *String*                     | The screen name you're tracking. We recommend human-readable names like **Login** or **Register**.                                                                     |
| `category` *String*                 | The screen category. If you're making a news app, the category could be **Sports**.                                                                                    |
| `properties` *Properties, optional* | A dictionary of properties for the screen view. If the screen is **Restaurant Reviews**, it might have properties like `reviewCount` or `restaurantName`.              |
| `options` *Options, optional*       | An `Options` object lets you set a [timestamp](#historical-import), [enable or disable destinations](#selecting-destinations), or [send additional context](#context). |

Find details on the **Screen payload** in our [Spec](/docs/segment/connections/spec/screen/).

## Group

Group lets you associate an [identified user](/docs/segment/connections/sources/catalog/libraries/server/java/#identify) user with a group. A group could be a company, organization, account, project or team! It also lets you record custom traits about the group, like industry or number of employees.

This is useful for tools like [Intercom](/docs/segment/connections/destinations/catalog/intercom/), [Preact](/docs/segment/connections/destinations/catalog/preact/) and [Totango](/docs/segment/connections/destinations/catalog/totango/), as it ties the user to a **group** of other users.

Example Group call:

```csharp
Analytics.Client.Group("userId", "groupId", new Traits() {
    { "name", "Initech, Inc." },
    { "website", "http://www.example.com" }
});
```

The Group call has the following fields:

| `userId` *String*             | The ID for this user in your database.                                                                                                                                 |
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `groupId` *String*            | The ID for this group in your database.                                                                                                                                |
| `traits` *Traits, optional*   | A dictionary of traits you know about the group. Things like: `name` or `website`.                                                                                     |
| `options` *Options, optional* | An `Options` object lets you set a [timestamp](#historical-import), [enable or disable destinations](#selecting-destinations), or [send additional context](#context). |

Find more details about Group including the **Group payload** in our [Spec](/docs/segment/connections/spec/group/).

## Alias

Alias is how you associate one identity with another. This is an advanced method, but it is required to manage user identities successfully in *some* of our destinations.

In [Mixpanel](/docs/segment/connections/destinations/catalog/mixpanel/#alias) it's used to associate an anonymous user with an identified user once they sign up. For [Kissmetrics](/docs/segment/connections/destinations/catalog/kissmetrics/#alias), if your user switches IDs, you can use 'alias' to rename the 'userId'.

Example Alias call:

```csharp
Analytics.Client.Alias("previousId", "userId");
```

Here's a full example of how we might use the Alias call:

```csharp
// the anonymous user does actions ...
Analytics.Client.Track("anonymous_user", "Anonymous Event");
// the anonymous user signs up and is aliased
Analytics.Client.Alias("anonymous_user", "identified@example.com");
// the identified user is identified
Analytics.Client.Identify("identified@example.com", new Traits() { plan: "Free" });
// the identified user does actions ...
Analytics.Client.Track("identified@example.com", "Identified Action");
```

For more details about Alias, including the **Alias call payload**, check out our [Spec](/docs/segment/connections/spec/alias/).

## Development settings

You can use this initialization during development while testing the library. `SetAsync(false)` will make sure the library makes a request to our servers every time it's called.

```csharp
Analytics.Initialize("YOUR_WRITE_KEY", new Config().SetAsync(false));
```

Don't forget to set async back to `true` for production, so that you can advantage of asynchronous flushing on a different thread.

## Options

An `Options` object lets you:

1. Set a [timestamp](#historical-import), [enable or disable destinations](#selecting-destinations)
2. [Send additional context](#context)
3. [Send an anonymousId](#anonymous-id)

## Selecting destinations

The Alias, Group, Identify, Page and Track calls can all be passed an object of `options` that lets you turn certain destinations on or off. By default all destinations are enabled.

Here's an example Identify call with the `options` object shown.

```csharp
Analytics.Client.Identify("hj2kf92ds212", new Traits() {
    { "email", "tom@example.com" },
    { "name", "Tom Smykowski" },
}, new Options()
    .SetIntegration("all", false)
    .SetIntegration("Kissmetrics", true)
);
```

In this case, we're specifying that we want this identify to only go to Kissmetrics. `"all", false` says that no destination should be enabled unless otherwise specified. `{ "Kissmetrics", true }` turns on Kissmetrics, etc.

destination flags are **case sensitive** and match [the destination's name in the docs](/docs/segment/connections/destinations/) (i.e. "AdLearn Open Platform", "awe.sm", "MailChimp", etc.).

**Note:** Available at the business level, filtering track calls can be done right from the Segment UI on your source schema page. We recommend using the UI if possible since it's a much simpler way of managing your filters and can be updated with no code changes on your side.

## Historical import

You can import historical data by adding the `timestamp` argument to your Identify and Track calls. *Note: If you're tracking things that are happening right now, leave out the timestamp and our servers will timestamp the requests for you.*

```csharp
Analytics.Client.Track("sadi89e2jd", "Logged Workout", new Properties() {
    { "distance", "10 miles" },
    { "city", "Boston" },
}, new Options()
    .SetTimestamp(new DateTime(2010, 1, 18))
);
```

## Context

If you're running a web server, you might want to send context variables such as `userAgent` or `ip` with your Page or Screen calls. You can do so by setting the `Context` in the `Options` object.

```csharp
Analytics.Client.Page("019mr8mf4r", "Login", new Properties() {
    { "path", "/login" },
    { "title", "Initech Login" }
}, new Options()
    .SetContext(new Context() {
        { "app", "Education App 2" }
    }));
```

Learn more on the [Context page](/docs/segment/connections/spec/common/#context).

## Anonymous ID

By default, the Xamarin library requires all messages to have a `userId`. If you would like to use an `anonymousId`, you can pass it in with options.

```csharp
Analytics.Client.Page(null, "Login", new Properties(), new Options()
    .SetAnonymousId("some-id"));
```

## Nested Properties

You can provide nested properties, like so:

```csharp
Analytics.Client.Identify("hj2kf92ds212", new Traits() {
    { "email", "tom@example.com" },
    { "name", "Tom Smykowski" },
    { "address", new Dict() {
        { "street", "123 Fake Street" },
        { "city", "Boston" }
    }}
});
```

## Batching

Our libraries are built to support high performance environments. That means it is safe to use Analytics.Xamarin on a web server that's serving hundreds of requests per second.

By default (in async mode), this library will start a single separate thread on initialization, and flush all messages on that thread. That means every method you call **does not** result in an HTTP request, but is queued in memory instead. Messages are flushed in batch in the background, which allows for much faster operation.

### How do I turn batching off?

Sometimes you might not want batching (eg. when debugging, or in short-lived programs). You can turn off batching by setting the `async` argument to `false`, and your requests will always be sent in a blocking manner.

```csharp
Analytics.Initialize("YOUR_WRITE_KEY", new Config().SetAsync(false));
```

### What happens if there are just too many messages?

If the module detects that it can't flush faster than it's receiving messages, it'll simply stop accepting messages. This means your program will never crash because of a backing up analytics queue. The maximum size of the queue defaults to `10000`, and here's how you can change it:

```csharp
Analytics.Initialize("YOUR_WRITE_KEY", new Config().SetMaxQueueSize(10000));
```

### How do I flush right now?!

You can also flush on demand. For example, at the end of your program, you'll want to flush to make sure there's nothing left in the queue. Just call the `Flush` method:

```csharp
Analytics.Client.Flush();
```

This method will block until all messages are flushed.

### How do I dispose of the flushing thread at the end of my program?

The Analytics client implements the `IDisposable` interface, and will turn off its flushing thread when you call `Dispose`.

```csharp
Analytics.Client.Dispose();
```

## Configuration

If you'd prefer to configure Analytics.Xamarin rather than rely on defaults:

```csharp
Analytics.Initialize("YOUR_WRITE_KEY", new Config()
    .SetAsync(true)
    .SetTimeout(TimeSpan.FromSeconds(10))
    .SetMaxQueueSize(10000));
```

| `SetAsync` *boolean*    | `true` to flush on a different thread, `false` to flush immediately on the same thread.    |
| ----------------------- | ------------------------------------------------------------------------------------------ |
| `SetTimeout` *TimeSpan* | The amount of time to wait before calling the HTTP request a timeout.                      |
| `SetMaxQueueSize` *int* | The maximum number of messages to allow into the queue before no new message are accepted. |

## Logging

`Analytics.Xamarin` has detailed logging, which you can enable by attaching your own handler, like so:

```csharp
using Segment;

Segment.Logger.Handlers += Logging_Handler;

void Logging_Handler(Level level, string message, Dict args) {
    if (args != null) {
        foreach (string key in args.Keys) {
            message += String.Format(" {0}: {1},", "" + key, "" + args[key]);
        }
    }
    Console.WriteLine(String.Format("[Analytics] [{0}] {1}", level, message));
}
```

## Anonymizing IP

We collect IP address for client-side (iOS, Android, Analytics.js and Xamarin) events automatically.

If you don't want us to record your tracked users' IP in destinations and S3, you can set your event's `context.ip` field to `0.0.0.0` . Our server won't record the IP address of the client for libraries if the `context.ip` field is already set.
