PostHog is available for install via Pub .
Set your PostHog API key and change the automatic event tracking 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.
To start, add posthog_flutter to your pubspec.yaml:
# rest of your code
dependencies :
flutter :
sdk : flutter
posthog_flutter : ^5.0.0
# rest of your code
Then complete the set up for each platform:
For Session replay and Surveys you must setup the SDK manually by disabling the com.posthog.posthog.AUTO_INIT mode.
There are 2 ways of initializing the SDK, automatically and manually.
Automatically:
Add your PostHog configuration to your AndroidManifest.xml file located in the android/app/src/main:
android/app/src/main/AndroidManifest.xml < manifest xmlns: android = " http://schemas.android.com/apk/res/android " package = " your.package.name " >
< application >
<!-- ... other configuration ... -->
< 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 = " https://us.i.posthog.com " /> <!-- usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' -->
< meta-data android: name = " com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS " android: value = " true " />
< meta-data android: name = " com.posthog.posthog.DEBUG " android: value = " true " />
</ application >
</ manifest >
Or manually (more control and more configurations available):
Add your PostHog configuration to your AndroidManifest.xml file located in the android/app/src/main:
android/app/src/main/AndroidManifest.xml < manifest xmlns: android = " http://schemas.android.com/apk/res/android " package = " your.package.name " >
< application >
<!-- ... other configuration ... -->
< meta-data android: name = " com.posthog.posthog.AUTO_INIT " android: value = " false " />
</ application >
</ manifest >
In both cases, you'll also need to update the minimum Android SDK version to 21 in android/app/build.gradle:
// rest of your config
defaultConfig {
minSdkVersion 21
// rest of your config
}
// rest of your config
There are 2 ways of initializing the SDK, automatically and manually.
You'll need to have Cocoapods installed.
Automatically:
Add your PostHog configuration to the Info.plist file located in the ios/Runner directory:
<?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 >
<!-- rest of your configuration -->
< key > com.posthog.posthog.API_KEY </ key >
< string > < ph_project_api_key > </ string >
< key > com.posthog.posthog.POSTHOG_HOST </ key >
< string > https://us.i.posthog.com </ string >
< key > com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS </ key >
< true />
< key > com.posthog.posthog.DEBUG </ key >
< true />
</ dict >
</ plist >
Or manually (more control and more configurations available):
Add your PostHog configuration to the Info.plist file located in the ios/Runner directory:
<?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 >
<!-- rest of your configuration -->
< key > com.posthog.posthog.AUTO_INIT </ key >
< false />
</ dict >
</ plist >
In both cases, you'll need to set the minimum platform version to iOS 13.0 in your Podfile:
platform : ios , '13.0'
# rest of your config
If you followed the automatic SDK setup, then there's no more configuration needed in Dart.
If you followed the manual SDK setup:
import 'package:flutter/material.dart' ;
import 'package:posthog_flutter/posthog_flutter.dart' ;
Future < void > main ( ) async {
// init WidgetsFlutterBinding if not yet
WidgetsFlutterBinding . ensureInitialized ( ) ;
final config = PostHogConfig ( '<ph_project_api_key>' ) ;
config . debug = true ;
config . captureApplicationLifecycleEvents = true ;
config . host = 'https://us.i.posthog.com' ;
await Posthog ( ) . setup ( config ) ;
runApp ( MyApp ( ) ) ;
}
For Web, add your Web snippet (which you can find in your project settings ) in the <header> of your web/index.html file:
<! DOCTYPE html >
< html >
< head >
<!-- ... other head elements ... -->
< script async >
! function ( t , e ) { var o , n , p , r ; e . __SV || ( window . posthog = e , e . _i = [ ] , e . init = function ( i , s , a ) { function g ( t , e ) { var o = e . split ( "." ) ; 2 == o . length && ( t = t [ o [ 0 ] ] , e = o [ 1 ] ) , t [ e ] = function ( ) { t . push ( [ e ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) } } ( p = t . createElement ( "script" ) ) . type = "text/javascript" , p . crossOrigin = "anonymous" , p . async = ! 0 , p . src = s . api_host + "/static/array.js" , ( r = t . getElementsByTagName ( "script" ) [ 0 ] ) . parentNode . insertBefore ( p , r ) ; var u = e ; for ( void 0 !== a ? u = e [ a ] = [ ] : a = "posthog" , u . people = u . people || [ ] , u . toString = function ( t ) { var e = "posthog" ; return "posthog" !== a && ( e += "." + a ) , t || ( e += " (stub)" ) , e } , u . people . toString = function ( ) { return u . toString ( 1 ) + ".people (stub)" } , o = "capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys getNextSurveyStep onSessionId" . split ( " " ) , n = 0 ; n < o . length ; n ++ ) g ( u , o [ n ] ) ; e . _i . push ( [ i , s , a ] ) } , e . __SV = 1 ) } ( document , window . posthog || [ ] ) ;
posthog . init ( '<ph_project_api_key>' , {
api_host : 'https://us.i.posthog.com' , // 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
defaults : '2025-11-30' ,
} )
</ script >
</ head >
<!-- other elements -->
</ html >
For more information please check: /docs/libraries/js