This is an optional library you can install if you're working with Flutter. It 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
Warning: This is a community maintained library. It is not officially supported by PostHog.
PostHog is available for install via Pub.
Configuration
Set your PostHog API key and change the automatic event tracking (only for Android and iOS) on if you wish the library to take care of it for you.
Remember that the application lifecycle events won't have any special context set for you by the time it is initialized. If you are using a self-hosted instance of PostHog you will need to have the public hostname or IP for your instance as well.
Android
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.posthog.posthog_flutter_example"><application><activity>[...]</activity><meta-data android:name="com.posthog.posthog.API_KEY" android:value="<ph_project_api_key>" /><meta-data android:name="com.posthog.posthog.POSTHOG_HOST" android:value="<ph_instance_address>" /><meta-data android:name="com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS" android:value="false" /><meta-data android:name="com.posthog.posthog.DEBUG" android:value="false" /></application></manifest>
iOS
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>[...]<key>com.posthog.posthog.API_KEY</key><string><ph_project_api_key></string><key>com.posthog.posthog.POSTHOG_HOST</key><string><ph_instance_address></string><key>com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS</key><false/><false/>[...]</dict></plist>
Web
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>example</title></head><body><script>!function () {var analytics = window.analytics = window.analytics || []; if (!analytics.initialize) if (analytics.invoked) window.console && console.error && console.error("PostHog snippet included twice."); else {analytics.invoked = !0; analytics.methods = ["captureSubmit", "captureClick", "captureLink", "captureForm", "pageview", "identify", "reset", "group", "capture", "ready", "alias", "debug", "page", "once", "off", "on"]; analytics.factory = function (t) { return function () { var e = Array.prototype.slice.call(arguments); e.unshift(t); analytics.push(e); return analytics } }; for (var t = 0; t < analytics.methods.length; t++) { var e = analytics.methods[t]; analytics[e] = analytics.factory(e) } analytics.load = function (t, e) { var n = document.createElement("script"); n.type = "text/javascript"; n.async = !0; n.src = "https://cdn.posthog.com/analytics.js/v1/" + t + "/analytics.min.js"; var a = document.getElementsByTagName("script")[0]; a.parentNode.insertBefore(n, a); analytics._loadOptions = e }; analytics.SNIPPET_VERSION = "4.1.0";analytics.load("YOUR_API_KEY_GOES_HERE");analytics.page();}}();</script><script src="main.dart.js" type="application/javascript"></script></body></html>
For more information please check: https://posthog.com/docs/integrate/client/js
Usage
To use this, add posthog_flutter
as a dependency in your pubspec.yaml file.
Supported methods
Method | Android | iOS | Web |
---|---|---|---|
identify | X | X | X |
capture | X | X | X |
screen | X | X | X |
alias | X | X | X |
getAnonymousId | X | X | X |
reset | X | X | X |
disable | X | X | |
enable | X | X | |
debug | X* | X | X |
setContext | X | X | |
isFeatureEnabled | X | X | X |
reloadFeatureFlags | X | X | X |
groups | X | X | X |
Debugging must be set as a configuration parameter in
AndroidManifest.xml
(see below). The Official PostHog Library does not offer the debug method for Android.
Example
import 'package:flutter/material.dart';import 'package:posthog_flutter/posthog_flutter.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {Widget build(BuildContext context) {Posthog.screen(screenName: 'Example Screen',);return MaterialApp(home: Scaffold(appBar: AppBar(title: Text('PostHog example app'),),body: Center(child: FlatButton(child: Text('TRACK ACTION WITH POSTHOG'),onPressed: () {Posthog.capture(eventName: 'button_clicked',properties: {'foo': 'bar','number': 1337,'clicked': true,'$set': {'userProp': 'value1','$groups': {'company': 'twitter',},},'$set_once': {'userProp': 'value2'}},);},),),),navigatorObservers: [PosthogObserver(),],);}}
Capturing events
You can send custom events using capture
:
import 'package:posthog_flutter/posthog_flutter.dart';Posthog.capture(eventName: 'user_signed_up',);
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:
Posthog.capture(eventName: 'user_signed_up',properties: {'login_type': 'email','is_free_trial': true});
Feature Flags
PostHog's feature flags enable you to safely deploy and roll back new features.
Boolean feature flags
Posthog.isFeatureEnabled(key: 'flag-key',);
Multivariate feature flags
Multivariate feature flags are not yet available in our Flutter SDK.
Reloading feature flags
Feature flag values are cached. If something has changed with your user and you'd like to refetch their flag values, call PostHog.reloadFeatureFlags()
.
Issues
Please file any issues, bugs, or feature requests in our GitHub repository.
Contributing
If you wish to contribute a change to this repo, please send a Pull Request.