Skip to content

Commit

Permalink
feature Posthog client lib for Flutter and Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
fuziontech committed Jun 25, 2020
0 parents commit 5558c49
Show file tree
Hide file tree
Showing 91 changed files with 4,315 additions and 0 deletions.
592 changes: 592 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 20e59316b8b8474554b38493b8ca888794b0234a
channel: stable

project_type: plugin
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}
72 changes: 72 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## 2.1.1

- fixed `context` overwriting (using `posthog.setContext(...)`) for `iOS` devices

## 2.1.0

- added `ENABLE_BRANCH_IO_INTEGRATION` configuration option

## 2.0.2

- added `disable` and `enable` methods for Android
- added `DEBUG` configuration option for Android (used on `AndroidManifest.xml`)

## 2.0.1

- fixed method channel issue

## 2.0.0

### BREAKING CHANGE

- removed `putDeviceToken` call
- added `setContext` as a better and more general approach to setting context variables

### Feature

- added support to the new [Android plugins APIs](https://flutter.dev/docs/development/packages-and-plugins/plugin-api-migration)

## 1.0.0

### BREAKING CHANGE

- the name was changed from `FlutterPosthog` to `Posthog`
- set flutter version to `>=1.12.13+hotfix.4 <2.0.0`

### Feature

- added web support

## 0.0.8

- added support for integration options in Android (iOS was already supported in previous versions)
- added a configuration parameter for enabling/disabling automatic application lifecycle tracking (only for Android, iOS)
- removed `Application Started` tracking event from Android plugin registration

## 0.0.7

- initialize plugin along with FirebaseMessaging

## 0.0.6

- added support for device token

## 0.0.5

- improved dependencies

## 0.0.4

- added NavigatorObserver to automatically track named screen transitions

## 0.0.3

- added package fixes and missing license

## 0.0.2

- extended README

## 0.0.1

- initial implementation to support general Posthog API for iOS and Android
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Posthog

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Posthog plugin

![Pub Version](https://img.shields.io/pub/v/flutter_posthog)

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

## Usage

To use this plugin, add `flutter_posthog` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).

### 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 | |

\* 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

```dart
import 'package:flutter/material.dart';
import 'package:flutter_posthog/flutter_posthog.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 write keys.

Set your Posthog write 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

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.posthog.flutter_posthog_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
<?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/>
<false/>
[...]
</dict>
</plist>
```

### Web

```html
<!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

## Sending device tokens strings for push notifications

Posthog integrates with 3rd parties that allow sending push notifications.
In order to do that you will need to provide it with the device token string - which can be obtained using one of the several Flutter libraries.

As soon as you obtain the device token string, you need to add it to Posthog's context by calling `setContext` and then emit a tracking event named `Application Opened` or `Application Installed`. The tracking event is needed because it is the [only moment when Posthog propagates it to 3rd parties](https://posthog.com/docs/connections/destinations/catalog/customer-io/).

Both calls (`setContext` and `track`) can be done sequentially at startup time, given that the token exists.
Nonetheless, if you don't want to delay the token propagation and don't mind having an extra `Application Opened` event in the middle of your app's events, it can be done right away when the token is acquired.

```dart
await Posthog.setContext({
'device': {
'token': yourTokenString
},
});
// the token is only propagated when one of two events are called:
// - Application Installed
// - Application Opened
await Posthog.capture(eventName: 'Application Opened');
```

A few important points:

- The token is propagated as-is to Posthog through the context field, without any manipulation or intermediate calls to Posthog's libraries. Strucutred data - such as APNs - need to be properly converted to its string representation beforehand
- On iOS, once the `device.token` is set, calling `setContext({})` will _not_ clean up its value. This occurs due to the existence of another method from posthog's library that sets the device token for Apple Push Notification service (APNs )
- `setContext` always overrides any previous values that were set in a previous call to `setContext`
- `setContext` is not persisted after the application is closed

## Issues

Please file any issues, bugs, or feature requests in the [GitHub repo](https://github.com/posthog/flutter-posthog/issues/new).

## Contributing

If you wish to contribute a change to this repo, please send a [pull request](https://github.com/posthog/flutter-posthog/pulls).
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
17 changes: 17 additions & 0 deletions android/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>android_</name>
<comment>Project android_ created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Library/Java/JavaVirtualMachines/openjdk-13.0.1.jdk/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
38 changes: 38 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
group 'com.posthog.flutter_posthog'
version '1.0-SNAPSHOT'

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}

dependencies {
implementation 'com.posthog.android:posthog:1.+'
}
}
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.gradle.jvmargs=-Xmx1536M

6 changes: 6 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Mon Sep 16 15:21:19 CEST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'flutter_posthog'
4 changes: 4 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.posthog.flutter_posthog">
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Loading

0 comments on commit 5558c49

Please sign in to comment.