Previous article: Part 2: Implementing Logger and Server Analysis
PostHog is a powerful open source analytics platform that provides comprehensive user behavior tracking. In this article, we will implement PostHog analysis in Flutter using our analysis architecture.
PostHog settings
You need to follow the following series of steps to prepare flutter for posthog.
1.PostHog account creation
access postg website And use email, google or github to register to create your account.
2. Project settings
When you open a new account, you’ll see the default items that are already turned on.
- Click Drop-down arrow next to name > Click The gear icon opens settings.
- Give your project a custom name.
- Scroll down and copy
API_KEY
value - Also note that if the area is us or European Union
3. Flutter package setting + configuration
- add package posthog_flutter for you
pubspec.yaml
. - Add the following lines to your
AndroidManifest.xml
in
Label
....
android:name="com.posthog.posthog.AUTO_INIT" android:value="false" />
The reason we disable AUTO_INIT is that we will provide configuration in the flutter code. This allows us to apply dynamic logic to flavors.
- Do the same thing in ios
Info.plist
document:
<dict>
....
<key>com.posthog.posthog.AUTO_INIT</key>
<false/>
</dict>
- exist
main.dart
Or whatever your entry file is, add the following code:
import 'package:posthog_flutter/posthog_flutter.dart';
....
Future<void> main() async {
WidgetsBinding.ensureInitialized();
final config = PostHogConfig(`<YOUR_API_KEY>`)
..debug = true
..captureApplicationLifecycleEvents = true // Allows tracking backgrounded, foregrounded etc. events
..host = 'https://us.i.posthog.com'; // or for EU Region 'https://eu.i.posthog.com'
await Posthog().setup(config);
runApp();
}
PostHog analytics implementation
our PosthogAnalyticsClient
implement AnalyticsClientBase
Signing a contract when using PostHog’s specific API:
Main features
- user identification
- event tracking
- screen tracking
- Custom properties
- User properties
- Session management
Attach users to analytics events
When you initially track events, they do so anonymously. Posthog provides ways to attach events to consumers and maintain a healthy session. To attach an event to the logged in user, you can use the identify() method:
await Posthog().identify(
userId: userId,
userProperties: { // optional
'email': email,
'role': role,
},
)
Thereafter, all tracked events will be automatically attached to your logged in user based on their userId
. If you wish to log out a user, be sure to call reset()
This way you can identify the next user to log in.
await Posthog().reset();
Track screen view
If you want to track screen changes, use:
_posthog.screen(
screenName: routeName,
properties: {'action': action},
)
custom event
Or for custom events use:
_posthog.capture(
eventName: 'button_pressed',
properties: {
'button_name': buttonName,
...?data,
},
);
Implementation Notes
- Using posthog in this way allows for maximum flexibility and provides a nice wrapper around the original approach.
Coming soon
exist Part 4: Establish a unified analytics service look and feelwe will bring everything together through facade services to seamlessly manage multiple analytics clients.