posthog_flutter 1.11.2
posthog_flutter: ^1.11.2 copied to clipboard

Flutter Android iOS web

Flutter implementation of Posthog client for iOS, Android and Web

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(),
      ],
    );
  }
}
2
likes
90
pub points
46%
popularity

Flutter implementation of Posthog client for iOS, Android and Web

Homepage
Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

Uploader

james.g@posthog.com

License

MIT (LICENSE)

Dependencies

flutter, flutter_web_plugins, meta

More

Packages that depend on posthog_flutter