Skip to content
ChangelogBook a demoSign up

Flutter SDK

The Flutter SDK makes it easy to track events from all types of Flutter applications.

Installation

The Flutter SDK is hosted on pub.dev.

You may install the SDK by adding it to your pubspec.yaml file:

flutter pub add hightouch_events

Initialization

You should initialize the SDK in the top-level app state and store it in a variable for use across your application. For example, you may want to initialize it in your app delegate.

Example:

final analytics = createClient(Configuration("WRITE_KEY"));

Configuration options:

OptionTypeDescription
writeKeyStringYour Hightouch write key.
apiHostStringThe API host to send the tracked events to.
trackApplicationLifecycleEventsBoolWhether to automatically track application lifecycle events (like opening the application). Defaults to false.
foregroundSessionTimeoutintMaximum foreground inactivity in milliseconds before a new session starts. Defaults to 1800000 (30 minutes).
backgroundSessionTimeoutintMaximum time in the background, in milliseconds, before a new session starts on resume. Defaults to 1800000 (30 minutes).

Session tracking

Session tracking is enabled by default. The SDK attaches sessionId, sessionStart, and a session object to context on every event so you can group events from the same app session.

Sessions use two timeouts, both in milliseconds:

  • foregroundSessionTimeout limits inactivity while the app is in the foreground.
  • backgroundSessionTimeout limits how long the app can stay in the background before the next resume starts a new session.

Both default to 1800000 (30 minutes). Set both to 0 to disable session tracking.

A new session starts when any of these occur:

  • First launch, when no session state is persisted.
  • Foreground inactivity exceeds foregroundSessionTimeout.
  • Resume after being backgrounded longer than backgroundSessionTimeout, including after process death.
  • Calling reset().

Session state is persisted across app relaunches. sessionIndex is a per-device lifetime counter. It starts at 0 and resets only when the app is uninstalled, not when you call reset().

You can change the timeouts when you initialize the SDK:

final analytics = createClient(Configuration("WRITE_KEY",
    foregroundSessionTimeout: 60 * 60 * 1000,
    backgroundSessionTimeout: 60 * 60 * 1000));

context.session includes:

FieldTypeDescription
sessionIdintCurrent session ID (epoch milliseconds).
sessionIndexintPer-device lifetime counter. Starts at 0.
sessionStartbooleanPresent and true only on the first event of the session.
eventIndexint0-based index of the event within the session.
previousSessionIdint or nullPrevious session ID. null on the first session.
firstEventIdUUIDmessageId of the session's first event.
firstEventTimestampISO 8601When the session began.

sessionId and sessionStart are also set on context for parity with the browser SDK. sessionStart is included only when it is true.

API

Identify

The analytics.identify method sends an identify event.

Example usage:

analytics.identify(userId: "user-123", userTraits: UserTraits(email: "user@example.com"));

Track

The analytics.track method sends a track event.

Example usage:

analytics.track("View Product", properties: {"productId": 123});

Screen

The analytics.screen method sends a screen event.

Example usage:

analytics.screen("Home");

Group

The analytics.group method sends a group event.

Example usage:

analytics.group("group-123", groupTraits: GroupTraits(name: "Group 123"));

Flush

The Flutter SDK buffers events locally before sending them to Hightouch's servers. This minimizes the number of requests made by the SDK and makes the tracking non-blocking.

To force the local buffer to be sent to Hightouch immediately call the flush() method.

Reset

The reset method resets the identify and group for the local session. Specifically, it resets the anonymousId, userId, referrer, and traits.

The reset method should be called when users log out. This way, if the user logs back in with another account, the userIds and traits from the different sessions remain separate. Calling reset() also starts a new analytics session. See Session tracking.

Ready to get started?

Jump right in or a book a demo. Your first destination is always free.

Book a demoSign upBook a demo

Need help?

Our team is relentlessly focused on your success. Don't hesitate to reach out!

Feature requests?

We'd love to hear your suggestions for integrations and other features.

Privacy PolicyTerms of Service