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:
npm install @capawesome/capacitor-posthog posthog-js
After installing the package, you need to update your Capacitor project:
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:
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
:
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
, orinvite sent
.
Setting event properties
Optionally, you can also include additional information in the event by setting the properties value:
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()
:
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:
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.