Tracking for MAUI SDK

Track custom events, identify and anonymize customers, manage sessions, track payments, and set default properties with the Bloomreach MAUI SDK in your app.

You can track events in Marketing to learn more about your app’s usage patterns and to segment your customers by their interactions.

By default, the SDK tracks certain events automatically, including:

  • Installation (after app installation and after invoking anonymize)
  • User session start and end
  • Banner event for showing an in-app message or content block

You can also track any custom event relevant to your business.

📘

Note

Also see Mobile SDK tracking FAQ at Bloomreach Support Help Center.

Events

Track event

Use the TrackEvent method with an Event object as an argument to track any custom event type relevant to your business.

You can use any name for a custom event type. We recommend using a descriptive and human-readable name.

For an overview of commonly used custom events, see the Custom events documentation.

Arguments

NameTypeDescription
evt (required)EventEvent object.
timestampdouble?Unix timestamp (in seconds) specifying when the event was tracked. The default value is the current time.
Event
NameTypeDescription
Name (required)StringName of the event type, for example screen_view.
AttributesDictionary<string, object?>Dictionary of event properties.

Examples

Imagine you want to track which screens a customer views. You can create a custom event screen_view for this.

First, create an Event with the name and a dictionary of the properties you want to track with it. In this example, you track the name of the screen, so you include the property screen_name along with any other relevant properties:

var evt = new Event("screen_view") { ["screen_name"] = "dashboard", ["other_property"] = 123.45 };

Pass the event object to TrackEvent as follows:

Bloomreach.BloomreachSDK.TrackEvent(evt);

The following example shows how to use a nested structure for complex properties:

var properties = new Dictionary<string, object?>
{
    ["purchase_status"] = "success",
    ["product_list"] = new object[] {
        new Dictionary<string, object>
        {
            ["product_id"] = "abc123",
            ["quantity"] = 2
        },
        new Dictionary<string, object>
        {
            ["product_id"] = "abc456",
            ["quantity"] = 1
        },
    },
    ["total_price"] = 7.99
};

var evt = new Event("purchase");
evt.Attributes = properties;
Bloomreach.BloomreachSDK.TrackEvent(evt);

If the event happened earlier, pass its time as a Unix timestamp in seconds:

var fiveMinutesAgo = DateTimeOffset.UtcNow.AddMinutes(-5).ToUnixTimeMilliseconds() / 1000.0;
Bloomreach.BloomreachSDK.TrackEvent(new Event("screen_view") { ["screen_name"] = "dashboard" }, fiveMinutesAgo);

Customers

Identifying your customers allows you to track them across devices and platforms, improving the quality of your customer data.

Without identification, events are tracked for an anonymous customer who is identified only by a cookie. Once the customer is identified by a hard ID, these events are transferred to a newly identified customer.

📘

Note

An app user and a customer record can be related by a soft or hard ID, but they're separate entities with their own lifecycles. Consider how the two lifecycles relate and when to use identify and anonymize.

Identify

Use the IdentifyCustomer method with a Customer object as an argument to identify a customer using their unique hard ID.

The default hard ID is registered and its value is typically the customer's email address. However, your Marketing project may define a different hard ID.

Optionally, you can track additional customer properties such as first and last names or age.

❗️

Warning

You can use IdentifyCustomer with a soft ID, but use caution. In some cases, for example after using Anonymize, this can associate the current user with an incorrect customer profile.

The SDK stores data, including the customer hard ID, in a local cache on the device. To remove the hard ID from the local cache, call anonymize in the app.

If you anonymize or delete the customer profile in the Marketing web app, a later SDK initialization in the app can reidentify or recreate the profile from the locally cached data.

Arguments

NameTypeDescription
customer (required)CustomerCustomer object.
Customer
NameTypeDescription
CustomerIds (required)Dictionary<String, String>Dictionary of customer unique identifiers. Only identifiers defined in the Marketing project are accepted.
PropertiesDictionary<string, object?>Dictionary of customer properties.

Examples

First, create a Customer object containing at least the customer's hard ID and, optionally, a dictionary with additional customer properties:

var customer = new Customer("[email protected]").
    WithProperties(new Dictionary<string, object>
    {
        ["first_name"] = "Jane",
        ["last_name"] = "Doe",
        ["age"] = 32
    });

Pass the customer object to IdentifyCustomer():

Bloomreach.BloomreachSDK.IdentifyCustomer(customer);

To update only the customer ID without any additional properties, leave those out:

var customer = new Customer("[email protected]");
Bloomreach.BloomreachSDK.IdentifyCustomer(customer);

Anonymize

Use the anonymize() method to delete all information stored locally and reset the current SDK state. A typical use case for this is when the user signs out of the app.

This method causes the SDK to:

  • Remove the push notification token for the current customer from local device storage and the customer profile in Marketing.
  • Clear local repositories and caches, excluding tracked events.
  • Track a new session start if automaticSessionTracking is enabled.
  • Create a new customer record in Marketing (a new cookie soft ID is generated).
  • Assign the previous push notification token to the new customer record.
  • Preload in-app messages, in-app content blocks, and App Inbox for the new customer.
  • Track a new installation event for the new customer.

You can also use the Anonymize method to switch to a different Marketing project. The SDK then tracks events to a new customer record in the new project, similar to the first app session after installation on a new device.

🚧

Avoid anonymous events on logout

Anonymize creates a new anonymous customer profile and tracks subsequent events against it. To avoid tracking events against an unidentified profile, call StopIntegration on logout instead. Unlike Anonymize, it doesn't create a new anonymous profile. This matters most for event stream integrations with SDK auth tokens on a stream that accepts only signed requests.

Examples

Bloomreach.BloomreachSDK.Anonymize();

Switch to a different project:

 Bloomreach.BloomreachSDK.Anonymize(
    new Project(
        "YOUR_PROJECT_TOKEN",
        "YOUR_API_KEY",
        "https://api.exponea.com"
    )
);

Switch to a different project and set its project mapping with the projectMapping argument:

Bloomreach.BloomreachSDK.Anonymize(
    new Project("YOUR_PROJECT_TOKEN", "YOUR_API_KEY", "https://api.exponea.com"),
    new Dictionary<EventType, IList<Project>>
    {
        {
            EventType.Payment,
            new List<Project> { new Project("PAYMENTS_PROJECT_TOKEN", "PAYMENTS_API_KEY", "https://api.exponea.com") }
        }
    }
);

Switch to an event stream (Data hub). Project mappings don't apply to streams. Without a base URL, the SDK uses its default https://api.exponea.com, not the base URL of the current integration. If you use a custom base URL, pass it:

Bloomreach.BloomreachSDK.AnonymizeToStream("YOUR_STREAM_ID");
Bloomreach.BloomreachSDK.AnonymizeToStream("YOUR_STREAM_ID", "YOUR_API_BASE_URL");

Get the customer cookie

Without a hard ID, the SDK tracks the customer by a cookie soft ID. Use GetCustomerCookie to get the cookie of the current customer, for example to share the customer's identity with a web view. Call it after you configure the SDK:

string? cookie = Bloomreach.BloomreachSDK.GetCustomerCookie();

The cookie stays the same when you identify the customer with a hard ID. Anonymize replaces it with a new one. StopIntegration and ClearLocalCustomerData delete it, and the SDK creates a new one when you configure it again.

Sessions

The SDK tracks sessions automatically by default, producing two events: session_start and session_end.

The session represents the actual time spent in the app. It starts when the app is launched and ends when it goes into the background. If the user returns before the session times out, the app continues the current session.

The default session timeout is 60 seconds. Set SessionTimeout in the SDK configuration to specify a different timeout.

After configuring the SDK, you can read and change the session timeout and turn automatic session tracking on or off at runtime:

Bloomreach.BloomreachSDK.SetSessionTimeout(TimeSpan.FromSeconds(120));
TimeSpan timeout = Bloomreach.BloomreachSDK.GetSessionTimeout();

Bloomreach.BloomreachSDK.SetAutomaticSessionTracking(false);
bool automatic = Bloomreach.BloomreachSDK.IsAutomaticSessionTracking();

On iOS, SetSessionTimeout also turns automatic session tracking on: the native iOS SDK sets the timeout only together with automatic session tracking.

Track session manually

To disable automatic session tracking, set AutomaticSessionTracking to false in the SDK configuration.

Use the TrackSessionStart and TrackSessionEnd methods to track sessions manually. If you call TrackSessionStart while a session is still open, the SDK first tracks session_end for it. To turn this off, set ManualSessionAutoClose to false in the configuration.

Examples

Bloomreach.BloomreachSDK.TrackSessionStart();
Bloomreach.BloomreachSDK.TrackSessionEnd();

Payments

The SDK provides a convenience method TrackPaymentEvent to help you track information about a payment for a product or service within the app.

Track payment event

Use the TrackPaymentEvent method to track payments. To support multiple platforms and use cases, the SDK defines a map of properties that contains basic information about the purchase.

Arguments

NameTypeDescription
payment (required)PaymentMap of payment properties.
timestampdouble?Unix timestamp (in seconds) specifying when the payment took place.

Payment

NameTypeDescription
value (required)doubleThe total purchase value.
currency (required)stringThe currency in which the purchase was made.
systemstring?
productIdstring?
productTitlestring?
receiptstring?

Examples

Bloomreach.BloomreachSDK.TrackPaymentEvent(new Payment(
    12.34,
    "EUR",
    "CardHolder",
    "abcd1234",
    "Best product",
    "INV_12345"
));

Default properties

You can configure default properties to be tracked with every event. If a tracked event has a property with the same key, it overwrites the default property value.

var config = new Configuration("YOUR_PROJECT_TOKEN", "YOUR_API_KEY", "YOUR_API_BASE_URL")
{
    DefaultProperties = new Dictionary<string, object>
    {
        { "thisIsADefaultStringProperty", "This is a default string value" },
        { "thisIsADefaultIntProperty", 1 },
        { "thisIsADefaultDoubleProperty", 12.53623 }
    }
};
Bloomreach.BloomreachSDK.Configure(config);

After initializing the SDK, you can change the default properties using SetDefaultProperties.

Bloomreach.BloomreachSDK.SetDefaultProperties(new Dictionary<string, object>()
{
    { "thisIsADefaultStringProperty", "This is a default string value" },
    { "thisIsADefaultIntProperty", 1},
    { "thisIsADefaultDoubleProperty", 12.53623}

});

SetDefaultProperties replaces all default properties. To read the current ones, use GetDefaultProperties:

IDictionary<string, object> defaultProperties = Bloomreach.BloomreachSDK.GetDefaultProperties();

Did this page help you?

© Bloomreach, Inc. All rights reserved.