Android Integration

Click here for the PostHog Android library. This is the official PostHog Android Library to capture and send events to any PostHog instance (including posthog.com).

This library uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your mobile app.

Installation

The best way to install the PostHog Android library is with a build system like Gradle. This ensures you can easily upgrade to the latest versions.

All you need to do is add the posthog module to your build.gradle:

dependencies {
  implementation 'com.posthog.android:posthog:1.+'
}

Configuration

The best place to initialize the client is in your Application subclass.

public class SampleApp extends Application {
  private static final String POSTHOG_API_KEY = "8jVz0YZ2YPtP7eL1I5l5RQIp-WcuFeD3pZO8c0YDMx4";
  private static final String POSTHOG_HOST = "https://app.posthog.com";

  @Override
  public void onCreate() {
    // Create a PostHog client with the given context, API key and host.
    PostHog posthog = new PostHog.Builder(this, POSTHOG_API_KEY, POSTHOG_HOST)
      .captureApplicationLifecycleEvents() // Record certain application events automatically!
      .recordScreenViews() // Record screen views automatically!
      .build();
    
    // Set the initialized instance as a globally accessible instance.
    PostHog.setSingletonInstance(posthog);

    // Now anytime you call PostHog.with, the custom instance will be returned.
    PostHog posthog = PostHog.with(this);
  }
}

Making calls

Identify

When you start tracking events with PostHog, each user gets an anonymous ID that is used to identify them in the system. In order to link this anonymous user with someone from your database, use the identify call.

Identify lets you add metadata to your users so you can easily identify who they are in PostHog, as well as do things like segment users by these properties.

An identify call requires:

  • distinctId which uniquely identifies your user in your database
  • userProperties with a dictionary of key:value pairs
PostHog.with(this)
       .identify(distinctID, new Properties()
                                .putValue("name", "My Name")
                                .putValue("email", "user@posthog.com"));

The most obvious place to make this call is whenever a user signs up, or when they update their information.

When you call identify, all previously tracked anonymous events will be linked to the user.

Capture

Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve, or find out where people are giving up.

A capture call requires:

  • event to specify the event name

    • We recommend naming events with "[noun] [verb]", such as movie played or movie updated, in order to easily identify what your events mean later on (we know this from experience).

Optionally you can submit:

  • properties, which can be an array with any information you'd like to add

For example:

PostHog.with(this)
       .capture("Button B Clicked", new Properties()
                                        .putValue("color", "blue")
                                        .putValue("icon", "new2-final"));

Flush

You can set the number of events in the configuration that should queue before flushing. Setting this to 1 will send events immediately and will use more battery. The default value for this is 20.

You can also configure the flush interval. By default we flush all events after 30 seconds, no matter how many events have been gathered.

PostHog posthog = new PostHog.Builder(this, POSTHOG_API_KEY, POSTHOG_HOST)
  .flushQueueSize(20)
  .flushInterval(30, TimeUnit.SECONDS)
  .build();

You can also manually flush the queue:

PostHog.with(this)
       .flush();

Reset

To reset the user's ID and anonymous ID, call reset. Usually you would do this right after the user logs out.

PostHog.with(this)
       .reset();

Sending screen views

With recordScreenViews(), PostHog will try to record all screen changes automatically.

If you want to manually send a new screen capture event, use the screen function.

This function requires a name. You may also pass in an optional properties object.

PostHog.with(this)
       .screen("Dashboard", new Properties()
                                  .putValue("background", "blue")
                                  .putValue("hero", "superhog"));

All Configuration Options

When creating the PostHog client, there are many options you can set:

PostHog posthog = new PostHog.Builder(this, POSTHOG_API_KEY, POSTHOG_HOST)
    // Record certain application events automatically! (off/false by default)
    .captureApplicationLifecycleEvents()

     // Record screen views automatically! (off/false by default)
    .recordScreenViews()

    // Capture deep links as part of the screen call. (off by default)
    .captureDeepLinks()

     // Maximum number of events to keep in queue before flushing (20)
    .flushQueueSize(int flushQueueSize)
 
    // Max delay before flushing the queue (30 seconds)
    .flushInterval(long flushInterval, TimeUnit timeUnit)
 
    // Enable or disable collection of ANDROID_ID (true)
    .collectDeviceId(boolean collect) 

    .build();

Thank You

This library is largely based on the analytics-android package.