posthog_flutter 1.10.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new62

Posthog plugin #

Pub Version

Flutter plugin to support iOS, Android and Web sources at https://posthog.com.

Usage #

To use this plugin, add posthog_flutter as a dependency in your pubspec.yaml file.

Supported methods #

MethodAndroidiOSWeb
identifyXXX
captureXXX
screenXXX
aliasXXX
getAnonymousIdXXX
resetXXX
disableXX
enableXX
debugX*XX
setContextXX

* 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 {
  @override
  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: 'ButtonClicked',
                properties: {
                  'foo': 'bar',
                  'number': 1337,
                  'clicked': true,
                },
              );
            },
          ),
        ),
      ),
      navigatorObservers: [
        PosthogObserver(),
      ],
    );
  }
}

Installation #

Setup your Android, iOS and/or web sources as described at Posthog.com and generate your api keys.

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 #

AndroidManifest.xml

<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="YOUR_API_KEY_GOES_HERE" />
        <meta-data android:name="com.posthog.posthog.POSTHOG_HOST" android:value="https://app.posthog.com" />
        <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 #

Info.plist

<?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>YOUR_API_KEY_GOES_HERE</string>
	<key>com.posthog.posthog.POSTHOG_HOST</key>
	<string>https://app.posthog.com</string>
	<key>com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS</key>
	<false/>
	[...]
</dict>
</plist>

Web #

<!DOCTYPE html>
<html>
  <head>
    [...]
  </head>
  <body>
    <script>
      !function(){ ...;
        posthog.init("YOUR_API_KEY_GOES_HERE", {api_host: 'https://app.posthog.com'});
        posthog.page();
      }}();
    </script>
    <script src="main.dart.js" type="application/javascript"></script>
  </body>
</html>

For more informations please check: https://posthog.com/docs/integrations/js-integration

Issues #

Please file any issues, bugs, or feature requests in the GitHub repo.

Contributing #

If you wish to contribute a change to this repo, please send a pull request.

1.10.0 #

  • We will include the last screen that you set in the capture events now. This will require users to user Posthog().capture() instead of Posthog.capture()

1.9.3 #

  • Bug fix for android identify method

1.9.2 #

  • Rename entire repo from flutter-posthog to posthog-flutter

1.9.1 #

  • Some renaming for consistency

1.9.0 #

  • Posthog client library for Flutter is released!

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:posthog_flutter/posthog_flutter.dart';

void main() {
  /// Wait until the platform channel is properly initialized so we can call
  /// `setContext` during the app initialization.
  WidgetsFlutterBinding.ensureInitialized();

  /// The `context.device.token` is a special property.
  /// When you define it, setting the context again with no token property (ex: `{}`)
  /// has no effect on cleaning up the device token.
  ///
  /// This is used as an example to allow you to set string-based
  /// device tokens, which is the use case when integrating with
  /// Firebase Cloud Messaging (FCM).
  ///
  /// This plugin currently does not support Apple Push Notification service (APNs)
  /// tokens, which are binary structures.
  ///
  /// Aside from this special use case, any other context property that needs
  /// to be defined (or re-defined) can be done.
  Posthog.setContext({
    'device': {
      'token': 'testing',
    }
  });

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Posthog.screen(
      screenName: 'Example Screen',
    );
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Posthog example app'),
        ),
        body: Column(
          children: <Widget>[
            Spacer(),
            Center(
              child: FlatButton(
                child: Text('CAPTURE ACTION WITH POSTHOG'),
                onPressed: () {
                  Posthog.capture(
                    eventName: 'ButtonClicked',
                    properties: {
                      'foo': 'bar',
                      'number': 1337,
                      'clicked': true,
                    },
                  );
                },
              ),
            ),
            Spacer(),
            Center(
              child: FlatButton(
                child: Text('Update Context'),
                onPressed: () {
                  Posthog.setContext({'custom': 123});
                },
              ),
            ),
            Spacer(),
            Center(
              child: FlatButton(
                child: Text('Clear Context'),
                onPressed: () {
                  Posthog.setContext({});
                },
              ),
            ),
            Spacer(),
            Center(
              child: FlatButton(
                child: Text('Disable'),
                onPressed: () async {
                  await Posthog.disable();
                  Posthog.capture(eventName: 'This event will not be logged');
                },
              ),
            ),
            Spacer(),
            Center(
              child: FlatButton(
                child: Text('Enable'),
                onPressed: () async {
                  await Posthog.enable();
                  Posthog.capture(eventName: 'Enabled capturing events!');
                },
              ),
            ),
            Spacer(),
            Platform.isIOS
                ? Center(
                    child: FlatButton(
                      child: Text('Debug mode'),
                      onPressed: () {
                        Posthog.debug(true);
                      },
                    ),
                  )
                : Container(),
            Spacer(),
          ],
        ),
      ),
      navigatorObservers: [
        PosthogObserver(),
      ],
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  posthog_flutter: ^1.10.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:posthog_flutter/posthog_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
24
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Jul 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:posthog_flutter/posthog_flutter.dart] that declares support for platforms: android, ios, web

Package does not support Flutter platform macos

Because of import path [package:posthog_flutter/posthog_flutter.dart] that declares support for platforms: android, ios, web

Package does not support Flutter platform windows

Because of import path [package:posthog_flutter/posthog_flutter.dart] that declares support for platforms: android, ios, web

Package not compatible with SDK dart

because of import path [posthog_flutter] that is in a package requiring null.

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:posthog_flutter/posthog_flutter.dartpackage:posthog_flutter/src/posthog.dartdart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_web_plugins 0.0.0
meta ^1.0.0 1.1.8
Transitive dependencies
collection 1.14.12 1.14.13
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test