Capacitor

Last updated:

|Edit this page
Which features are available in this library?
  • Event capture
  • Autocapture
  • User identification
  • Session recording
  • Feature flags
  • Group analytics

PostHog makes it easy to capture data from your Capacitor app, no matter the platform. You can find out more in the plugin's documentation.

Installation

Run the following command to install the package:

Terminal
npm install @capawesome/capacitor-posthog posthog-js

After installing the package, you need to update your Capacitor project:

Terminal
npx cap update

Configuration

To use the plugin, you need to call the setup(...) method with your PostHog project API key and host which you can find in your project settings:

TypeScript
import { Posthog } from '@capawesome/capacitor-posthog';
const setup = async () => {
await Posthog.setup({
apiKey: '<ph_project_api_key>',
host: 'https://us.i.posthog.com',
});
};

You pass in a host key to specify the PostHog instance you want to send events to. The default host is https://us.i.posthog.com.

Capturing events

You can send custom events using capture:

JavaScript
await Posthog.capture({
event: 'test-event',
});

Tip: We recommend using a [object] [verb] format for your event names, where [object] is the entity that the behavior relates to, and [verb] is the behavior itself. For example, project created, user signed up, or invite sent.

Setting event properties

Optionally, you can also include additional information in the event by setting the properties value:

JavaScript
await Posthog.capture({
event: 'test-event',
properties: {
key: 'value',
},
});

Feature flags

PostHog's feature flags enable you to safely deploy and roll back new features.

Feature flags are not yet supported by this library, but you can still use them via the API. If you would like to contribute, please check out the GitHub repository.

Group analytics

Group analytics enable you to associate the events for that person's session with a group (e.g. teams, organizations, etc.). Read the group analytics docs guide for more information.

Note: This is a paid feature and is not available on the open-source or free cloud plan. Learn more here.

To associate the events for this session with a group, call group():

JavaScript
import { Posthog } from '@capawesome/capacitor-posthog';
const group = async () => {
await Posthog.group({
type: 'company',
key: 'company_id_in_your_db',
});
};

To associate the events for this session with a group and update the properties of that group, include the groupProperties property:

JavaScript
import { Posthog } from '@capawesome/capacitor-posthog';
const group = async () => {
await Posthog.group({
type: 'company',
key: 'company_id_in_your_db',
groupProperties: {
name: 'Awesome Inc.',
},
});
};

The name is a special property which is used in the PostHog UI for the name of the group. If you don't specify a name property, the group ID will be used instead.

Credits

This library was built by the Capawesome team. It is not maintained by the PostHog core team. If you have any questions or issues, please create a discussion or an issue in the GitHub repository.

Questions?

Was this page useful?

Next article

Elixir

This library provides an Elixir HTTP client for PostHog. See the repository for more information. Installation Capturing events Feature flags Feature flags are not supported yet in our Elixir SDK. However, you can integrate them into your project by using the PostHog API . Thanks Thanks to nkezhaya for contributing this library.

Read next article