├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ ├── config.yml │ ├── feature_request.yml │ └── maintainer-blank.yml ├── dependabot.yml ├── pull_request_template.md └── workflows │ ├── ci.yml │ ├── danger.yml │ └── publish.yml ├── .gitignore ├── .metadata ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── RELEASING.md ├── analysis_options.yaml ├── android ├── .gitignore ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ └── gradle-wrapper.properties ├── settings.gradle └── src │ ├── main │ ├── AndroidManifest.xml │ └── kotlin │ │ └── com │ │ └── posthog │ │ └── flutter │ │ ├── PostHogVersion.kt │ │ ├── PosthogFlutterPlugin.kt │ │ └── SnapshotSender.kt │ └── test │ └── kotlin │ └── com │ └── posthog │ └── flutter │ └── PosthogFlutterPluginTest.kt ├── dangerfile.js ├── example ├── .gitignore ├── .metadata ├── README.md ├── analysis_options.yaml ├── android │ ├── .gitignore │ ├── app │ │ ├── build.gradle │ │ └── src │ │ │ ├── debug │ │ │ └── AndroidManifest.xml │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── kotlin │ │ │ │ └── com │ │ │ │ │ └── example │ │ │ │ │ └── flutter │ │ │ │ │ └── MainActivity.kt │ │ │ └── res │ │ │ │ ├── drawable-v21 │ │ │ │ └── launch_background.xml │ │ │ │ ├── drawable │ │ │ │ └── launch_background.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── values-night │ │ │ │ └── styles.xml │ │ │ │ └── values │ │ │ │ └── styles.xml │ │ │ └── profile │ │ │ └── AndroidManifest.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ └── gradle-wrapper.properties │ └── settings.gradle ├── assets │ ├── posthog_logo.png │ └── training_posthog.png ├── ios │ ├── .gitignore │ ├── Flutter │ │ ├── AppFrameworkInfo.plist │ │ ├── Debug.xcconfig │ │ └── Release.xcconfig │ ├── Podfile │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ └── WorkspaceSettings.xcsettings │ ├── Runner │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ │ ├── AppIcon.appiconset │ │ │ │ ├── Contents.json │ │ │ │ ├── Icon-App-1024x1024@1x.png │ │ │ │ ├── Icon-App-20x20@1x.png │ │ │ │ ├── Icon-App-20x20@2x.png │ │ │ │ ├── Icon-App-20x20@3x.png │ │ │ │ ├── Icon-App-29x29@1x.png │ │ │ │ ├── Icon-App-29x29@2x.png │ │ │ │ ├── Icon-App-29x29@3x.png │ │ │ │ ├── Icon-App-40x40@1x.png │ │ │ │ ├── Icon-App-40x40@2x.png │ │ │ │ ├── Icon-App-40x40@3x.png │ │ │ │ ├── Icon-App-60x60@2x.png │ │ │ │ ├── Icon-App-60x60@3x.png │ │ │ │ ├── Icon-App-76x76@1x.png │ │ │ │ ├── Icon-App-76x76@2x.png │ │ │ │ └── Icon-App-83.5x83.5@2x.png │ │ │ └── LaunchImage.imageset │ │ │ │ ├── Contents.json │ │ │ │ ├── LaunchImage.png │ │ │ │ ├── LaunchImage@2x.png │ │ │ │ ├── LaunchImage@3x.png │ │ │ │ └── README.md │ │ ├── Base.lproj │ │ │ ├── LaunchScreen.storyboard │ │ │ └── Main.storyboard │ │ ├── Info.plist │ │ ├── Runner-Bridging-Header.h │ │ └── main.m │ └── RunnerTests │ │ └── RunnerTests.swift ├── lib │ └── main.dart ├── macos │ ├── .gitignore │ ├── Flutter │ │ ├── Flutter-Debug.xcconfig │ │ ├── Flutter-Release.xcconfig │ │ └── GeneratedPluginRegistrant.swift │ ├── Podfile │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── Runner │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── app_icon_1024.png │ │ │ ├── app_icon_128.png │ │ │ ├── app_icon_16.png │ │ │ ├── app_icon_256.png │ │ │ ├── app_icon_32.png │ │ │ ├── app_icon_512.png │ │ │ └── app_icon_64.png │ │ ├── Base.lproj │ │ └── MainMenu.xib │ │ ├── Configs │ │ ├── AppInfo.xcconfig │ │ ├── Debug.xcconfig │ │ ├── Release.xcconfig │ │ └── Warnings.xcconfig │ │ ├── DebugProfile.entitlements │ │ ├── Info.plist │ │ ├── MainFlutterWindow.swift │ │ └── Release.entitlements ├── pubspec.yaml └── web │ ├── favicon.png │ ├── icons │ ├── Icon-192.png │ ├── Icon-512.png │ ├── Icon-maskable-192.png │ └── Icon-maskable-512.png │ ├── index.html │ └── manifest.json ├── ios ├── .gitignore ├── Assets │ └── .gitkeep ├── Classes │ ├── PostHogFlutterVersion.swift │ └── PosthogFlutterPlugin.swift ├── Resources │ └── PrivacyInfo.xcprivacy └── posthog_flutter.podspec ├── ktlint-baseline.xml ├── lib ├── posthog_flutter.dart ├── posthog_flutter_web.dart └── src │ ├── posthog.dart │ ├── posthog_config.dart │ ├── posthog_flutter_io.dart │ ├── posthog_flutter_platform_interface.dart │ ├── posthog_flutter_web_handler.dart │ ├── posthog_observer.dart │ ├── posthog_widget.dart │ ├── replay │ ├── change_detector.dart │ ├── element_parsers │ │ ├── element_data.dart │ │ ├── element_data_factory.dart │ │ ├── element_object_parser.dart │ │ ├── element_parser.dart │ │ ├── element_parser_factory.dart │ │ ├── element_parsers_const.dart │ │ ├── image_element │ │ │ ├── position_calculator.dart │ │ │ ├── render_image_parser.dart │ │ │ └── scaler.dart │ │ └── root_element_provider.dart │ ├── image_extension.dart │ ├── mask │ │ ├── image_mask_painter.dart │ │ ├── posthog_mask_controller.dart │ │ ├── posthog_mask_widget.dart │ │ └── widget_elements_decipher.dart │ ├── native_communicator.dart │ ├── screenshot │ │ ├── screenshot_capturer.dart │ │ └── snapshot_manager.dart │ ├── size_extension.dart │ └── vendor │ │ └── equality.dart │ └── util │ ├── logging.dart │ ├── platform_io_real.dart │ └── platform_io_stub.dart ├── macos ├── Classes │ ├── PostHogFlutterVersion.swift │ └── PosthogFlutterPlugin.swift ├── Resources │ └── PrivacyInfo.xcprivacy └── posthog_flutter.podspec ├── pubspec.yaml ├── scripts ├── bump-version.sh ├── commit-code.sh └── prepare-release.sh └── test ├── posthog_flutter_platform_interface_fake.dart └── posthog_observer_test.dart /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @marandaneto 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: 🐞 Bug Report 2 | description: Tell us about something that's not working the way we (probably) intend. 3 | labels: ["bug"] 4 | body: 5 | 6 | 7 | - type: input 8 | id: version 9 | attributes: 10 | label: Version 11 | description: SDK Version 12 | placeholder: 3.0.0 ← should look like this 13 | validations: 14 | required: true 15 | 16 | - type: textarea 17 | id: repro 18 | attributes: 19 | label: Steps to Reproduce 20 | description: How can we see what you're seeing? Specific is terrific. 21 | placeholder: |- 22 | 1. foo 23 | 2. bar 24 | 3. baz 25 | validations: 26 | required: true 27 | 28 | - type: textarea 29 | id: expected 30 | attributes: 31 | label: Expected Result 32 | validations: 33 | required: true 34 | 35 | - type: textarea 36 | id: actual 37 | attributes: 38 | label: Actual Result 39 | description: Logs? Screenshots? Yes, please. 40 | validations: 41 | required: true 42 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Ask in the forums 4 | url: https://posthog.com/questions 5 | about: A place to ask questions. 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: 💡 Feature Request 2 | description: Tell us about a problem our SDK could solve but doesn't. 3 | labels: ["enhancement"] 4 | body: 5 | - type: textarea 6 | id: problem 7 | attributes: 8 | label: Problem Statement 9 | description: What problem could we solve that it doesn't? 10 | placeholder: |- 11 | I want to make whirled peas, but it doesn't blend. 12 | validations: 13 | required: true 14 | 15 | - type: textarea 16 | id: expected 17 | attributes: 18 | label: Solution Brainstorm 19 | description: We know you have bright ideas to share ... share away, friend. 20 | placeholder: |- 21 | Add a blender to it. 22 | validations: 23 | required: false 24 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/maintainer-blank.yml: -------------------------------------------------------------------------------- 1 | name: Blank Issue 2 | description: Blank Issue. Reserved for maintainers. 3 | body: 4 | - type: textarea 5 | id: description 6 | attributes: 7 | label: Description 8 | description: Please describe the issue. 9 | validations: 10 | required: true 11 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: weekly 7 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## :bulb: Motivation and Context 2 | 3 | 4 | 5 | 6 | ## :green_heart: How did you test it? 7 | 8 | 9 | ## :pencil: Checklist 10 | 11 | 12 | - [ ] I reviewed the submitted code. 13 | - [ ] I added tests to verify the changes. 14 | - [ ] I updated the docs if needed. 15 | - [ ] No breaking change or entry added to the changelog. 16 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | paths-ignore: 8 | - "**/*.md" 9 | 10 | # Publish using custom workflow 11 | jobs: 12 | build: 13 | runs-on: macos-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | 17 | - name: 'Set up Java' 18 | uses: actions/setup-java@v4 19 | with: 20 | java-version: 11 21 | distribution: 'temurin' 22 | 23 | - uses: dart-lang/setup-dart@v1 24 | - uses: subosito/flutter-action@v2 25 | with: 26 | channel: 'stable' 27 | 28 | - name: Install dependencies 29 | run: | 30 | flutter pub get 31 | cd example 32 | flutter pub get 33 | 34 | - name: SDK format check 35 | run: | 36 | make installLinters 37 | make checkFormatDart 38 | make analyzeDart 39 | make formatKotlin 40 | make formatSwift 41 | 42 | - name: Test 43 | run: flutter test 44 | 45 | - name: Build iOS 46 | working-directory: ./example 47 | run: flutter build ios --simulator --no-codesign 48 | 49 | - name: Build macOS 50 | working-directory: ./example 51 | run: flutter build macos 52 | 53 | - name: Build Android 54 | working-directory: ./example 55 | run: flutter build apk 56 | 57 | - name: Build Web 58 | working-directory: ./example 59 | run: flutter build web 60 | -------------------------------------------------------------------------------- /.github/workflows/danger.yml: -------------------------------------------------------------------------------- 1 | name: "Danger" 2 | on: 3 | pull_request: 4 | types: [opened, synchronize, reopened, edited, ready_for_review] 5 | 6 | jobs: 7 | build: 8 | name: Changelog 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | with: 13 | fetch-depth: 0 14 | - run: npx danger ci 15 | env: 16 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 17 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # .github/workflows/publish.yml 2 | name: Publish to pub.dev 3 | 4 | on: 5 | push: 6 | tags: 7 | - '[0-9]+.[0-9]+.[0-9]+*' # tag pattern on pub.dev: eg '3.0.0' 8 | 9 | # Publish using custom workflow 10 | jobs: 11 | publish: 12 | permissions: 13 | id-token: write # Required for authentication using OIDC 14 | runs-on: ubuntu-latest 15 | # use dart-lang/setup-dart/.github/workflows/publish.yml@v1 when https://github.com/dart-lang/setup-dart/issues/68 is fixed 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: dart-lang/setup-dart@v1 19 | - uses: subosito/flutter-action@v2 20 | with: 21 | channel: 'stable' 22 | - name: Install dependencies 23 | run: flutter pub get 24 | - name: Publish 25 | run: flutter pub publish --force 26 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: "d211f42860350d914a5ad8102f9ec32764dc6d06" 8 | channel: "stable" 9 | 10 | project_type: plugin 11 | 12 | # Tracks metadata for the flutter migrate command 13 | migration: 14 | platforms: 15 | - platform: root 16 | create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 17 | base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 18 | - platform: android 19 | create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 20 | base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 21 | - platform: ios 22 | create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 23 | base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 24 | - platform: web 25 | create_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 26 | base_revision: d211f42860350d914a5ad8102f9ec32764dc6d06 27 | 28 | # User provided section 29 | 30 | # List of Local paths (relative to this file) that should be 31 | # ignored by the migrate tool. 32 | # 33 | # Files that are not part of the templates will be ignored by default. 34 | unmanaged_files: 35 | - "lib/main.dart" 36 | - "ios/Runner.xcodeproj/project.pbxproj" 37 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## Next 2 | 3 | ## 5.0.0 4 | 5 | - chore: support flutter web wasm builds ([#112](https://github.com/PostHog/posthog-flutter/pull/112)) 6 | 7 | ### Breaking changes 8 | 9 | - Dart min version 3.3.0 10 | - Flutter min version 3.19.0 11 | 12 | ## 4.11.0 13 | 14 | - chore: Session Replay - GA ([#178](https://github.com/PostHog/posthog-flutter/pull/178)) 15 | 16 | ## 4.10.8 17 | 18 | - chore: pin the iOS SDK to 3.22.x ([#177](https://github.com/PostHog/posthog-flutter/pull/177)) 19 | 20 | ## 4.10.7 21 | 22 | - fix: import dart io only on non-web platforms ([#176](https://github.com/PostHog/posthog-flutter/pull/176)) 23 | 24 | ## 4.10.6 25 | 26 | - fix: check if image size is valid before sending snapshot ([#174](https://github.com/PostHog/posthog-flutter/pull/174)) 27 | 28 | ## 4.10.5 29 | 30 | - chore: linux and windows NoOp support ([#173](https://github.com/PostHog/posthog-flutter/pull/173)) 31 | 32 | ## 4.10.4 33 | 34 | - fix: dispose recorder if masking is disabled ([#166](https://github.com/PostHog/posthog-flutter/pull/166)) 35 | 36 | ## 4.10.3 37 | 38 | - chore: pin the iOS SDK to 3.x.x ([#162](https://github.com/PostHog/posthog-flutter/pull/162)) 39 | 40 | ## 4.10.2 41 | 42 | - chore: pin the iOS SDK to 3.19.x ([#157](https://github.com/PostHog/posthog-flutter/pull/157)) 43 | 44 | ## 4.10.1 45 | 46 | - fix: isSessionReplayActive returns false by default for flutter web ([#158](https://github.com/PostHog/posthog-flutter/pull/158)) 47 | 48 | ## 4.10.0 49 | 50 | - chore: add support for session replay manual masking with the PostHogMaskWidget widget ([#153](https://github.com/PostHog/posthog-flutter/pull/153)) 51 | 52 | ## 4.9.4 53 | 54 | - fix: solve masks out of sync when moving too fast ([#147](https://github.com/PostHog/posthog-flutter/pull/147)) 55 | 56 | ## 4.9.3 57 | 58 | - chore: pin the iOS SDK to 3.18.0 ([#149](https://github.com/PostHog/posthog-flutter/pull/149)) 59 | 60 | ## 4.9.2 61 | 62 | - chore: improve error logging when capturing snapshots ([#146](https://github.com/PostHog/posthog-flutter/pull/146)) 63 | 64 | ## 4.9.1 65 | 66 | - fix: blank screen when viewing session replay recordings ([#139](https://github.com/PostHog/posthog-flutter/pull/139)) 67 | 68 | ## 4.9.0 69 | 70 | - feat: add getter for current session identifier ([#134](https://github.com/PostHog/posthog-flutter/pull/134)) 71 | 72 | ## 4.8.0 73 | 74 | - chore: change screenshots debouncing approach to throttling ([#131](https://github.com/PostHog/posthog-flutter/pull/131)) 75 | - Added `throttleDelay` config and deprecated `debouncerDelay` config. 76 | 77 | ## 4.7.1 78 | 79 | - chore: do not send repeated snapshots ([#126](https://github.com/PostHog/posthog-flutter/pull/126)) 80 | 81 | ## 4.7.0 82 | 83 | - chore: flutter session replay (Android and iOS) ([#123](https://github.com/PostHog/posthog-flutter/pull/123)) 84 | - [Session replay docs](https://posthog.com/docs/session-replay/mobile), [PR pending review](https://github.com/PostHog/posthog.com/pull/10042) 85 | - Thanks @thisames for the [PR](https://github.com/PostHog/posthog-flutter/pull/116)! 86 | 87 | ## 4.6.0 88 | 89 | - chore: change host to new address ([#106](https://github.com/PostHog/posthog-flutter/pull/106)) 90 | - chore: allow manual initialization of the SDK ([#117](https://github.com/PostHog/posthog-flutter/pull/117)) 91 | 92 | ## 4.5.0 93 | 94 | - add PrivacyInfo for macOS ([#105](https://github.com/PostHog/posthog-flutter/pull/105)) 95 | 96 | ## 4.4.1 97 | 98 | - fix: const `defaultHost` was renamed to `DEFAULT_HOST` and broke the Android build ([#98](https://github.com/PostHog/posthog-flutter/issues/98)) 99 | 100 | ## 4.4.0 101 | 102 | - chore: Allow overriding the route filtering using a ctor param `routeFilter` ([#95](https://github.com/PostHog/posthog-flutter/pull/95)) 103 | 104 | ```dart 105 | bool myRouteFilter(Route? route) => 106 | route is PageRoute || route is OverlayRoute; 107 | final observer = PosthogObserver(routeFilter: myRouteFilter); 108 | ``` 109 | 110 | ## 4.3.0 111 | 112 | - add PrivacyInfo ([#94](https://github.com/PostHog/posthog-flutter/pull/94)) 113 | 114 | ## 4.2.0 115 | 116 | - add flush method ([#92](https://github.com/PostHog/posthog-flutter/pull/92)) 117 | 118 | ## 4.1.0 119 | 120 | - add unregister method ([#86](https://github.com/PostHog/posthog-flutter/pull/86)) 121 | 122 | ## 4.0.1 123 | 124 | - Fix passing optional values to the JS SDK ([#84](https://github.com/PostHog/posthog-flutter/pull/84)) 125 | 126 | ## 4.0.0 127 | 128 | - Android minSdkVersion 21 129 | - iOS min version 13.0 130 | - Flutter min version 3.3.0 131 | - Upgraded PostHog Android SDK to [v3](https://github.com/PostHog/posthog-android/blob/main/USAGE.md) 132 | - Upgraded PostHog iOS SDK to [v3](https://github.com/PostHog/posthog-ios/blob/main/USAGE.md) 133 | - Upgraded PostHog JS SDK to the latest version 134 | - PostHog Flutter Plugins are written in Kotlin and Swift 135 | - Added missing features such as feature flags payloads, debug, and more 136 | 137 | ## 4.0.0-RC.2 138 | 139 | - Upgrade iOS SDK to [3.1.0](https://github.com/PostHog/posthog-ios/releases/tag/3.1.0) [#79](https://github.com/PostHog/posthog-flutter/pull/79) 140 | 141 | ## 4.0.0-RC.1 142 | 143 | - Upgrade iOS SDK to [3.0.0](https://github.com/PostHog/posthog-ios/releases/tag/3.0.0) [#78](https://github.com/PostHog/posthog-flutter/pull/78) 144 | 145 | ## 4.0.0-beta.2 146 | 147 | - Flutter macOS support [#76](https://github.com/PostHog/posthog-flutter/pull/76) 148 | 149 | ## 4.0.0-beta.1 150 | 151 | - Record the root view as `root ('/')` instead of not recording at all [#74](https://github.com/PostHog/posthog-flutter/pull/74) 152 | - Do not mutate the given properties when calling capture [#74](https://github.com/PostHog/posthog-flutter/pull/74) 153 | - Thanks @lukepighetti for the [PR](https://github.com/PostHog/posthog-flutter/pull/66)! 154 | - Fix `CAPTURE_APPLICATION_LIFECYCLE_EVENTS` typo for iOS [#74](https://github.com/PostHog/posthog-flutter/pull/74) 155 | - Added iOS support for the `DEBUG` config [#74](https://github.com/PostHog/posthog-flutter/pull/74) 156 | - Upgrade iOS SDK that fixes missing `Application Opened` events [#74](https://github.com/PostHog/posthog-flutter/pull/74) 157 | 158 | ## 4.0.0-alpha.2 159 | 160 | - Internal changes only 161 | 162 | ## 4.0.0-alpha.1 163 | 164 | - Migrate to the new SDKs and latest tooling [#70](https://github.com/PostHog/posthog-flutter/pull/70) 165 | - Added missing features such as feature flags payloads, debug, and more 166 | 167 | ### Breaking changes 168 | 169 | - Android minSdkVersion 21 170 | - iOS min version 13.0 171 | - Flutter min version 3.3.0 172 | - Upgraded PostHog Android SDK to [v3](https://github.com/PostHog/posthog-android/blob/main/USAGE.md) 173 | - Upgraded PostHog iOS SDK to [v3 preview](https://github.com/PostHog/posthog-ios/blob/main/USAGE.md) 174 | - Upgraded PostHog JS SDK to the latest version 175 | - PostHog Flutter Plugins are written in Kotlin and Swift 176 | 177 | ### Acknowledgements 178 | 179 | Thanks @nehemiekoffi for the initial PR! 180 | 181 | ## 3.3.0 182 | 183 | - Migrate to Java 8 and minSdkVersion 19 [#54](https://github.com/PostHog/posthog-flutter/pull/54) 184 | 185 | ## 3.2.0 186 | 187 | - Add support to Dart v3.0.0 [#52](https://github.com/PostHog/posthog-flutter/pull/52) 188 | 189 | ## 3.1.0 190 | 191 | - Adds support for `groups` 192 | - Fixes a type issue with identify so that the userId is now always a String 193 | 194 | ## 3.0.5 195 | 196 | - Fixes a bug with the iOS implementation for feature flags that stopped the SDK from building 197 | 198 | ## 3.0.4 199 | 200 | - Adds CI/CD for deploying to pub.dev 201 | 202 | ## 3.0.0 203 | 204 | - Adds basic feature flags support with `isFeatureEnabled` and `reloadFeatureFlags` 205 | 206 | ## 2.0.3 207 | 208 | - Bugfixes with flutter web and identify call https://github.com/PostHog/posthog-flutter/pull/16 209 | 210 | ## 2.0.2 211 | 212 | - Update to androidX for example android project 213 | - Fix ios example app and params 214 | - Fix web library, example, and docs 215 | 216 | ## 2.0.1 217 | 218 | - Remove `generated_plugin_registrant.dart` from library 219 | 220 | ## 2.0.0 221 | 222 | - Migrate to flutter 2 223 | 224 | ## 1.11.2 225 | 226 | - Bump and pin version for Android lib to 1.1.1 because of bug 227 | 228 | ## 1.11.1 229 | 230 | - Bump and pin version for Android lib to 1.1.0 231 | 232 | ## 1.11.0 233 | 234 | - Bump the version for Android lib for screen \$screen_name consistency 235 | 236 | ## 1.10.0 237 | 238 | - We will include the last screen that you set in the capture events now. 239 | This will require users to user `Posthog().capture()` instead of `Posthog.capture()` 240 | 241 | ## 1.9.3 242 | 243 | - Bug fix for android identify method 244 | 245 | ## 1.9.2 246 | 247 | - Rename entire repo from flutter-posthog to posthog-flutter 248 | 249 | ## 1.9.1 250 | 251 | - Some renaming for consistency 252 | 253 | ## 1.9.0 254 | 255 | - PostHog client library for Flutter is released! 256 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | If you wish to contribute a change to this repo, please send a [pull request](https://github.com/posthog/posthog-flutter/pulls). 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 PostHog 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: formatKotlin formatSwift formatDart checkDart installLinters 2 | 3 | installLinters: 4 | brew install ktlint 5 | brew install swiftformat 6 | 7 | formatKotlin: 8 | ktlint --format --baseline=ktlint-baseline.xml 9 | 10 | # swiftlint ios/Classes --fix conflicts with swiftformat 11 | formatSwift: 12 | swiftformat ios/Classes --swiftversion 5.3 13 | 14 | formatDart: 15 | dart format . 16 | 17 | checkFormatDart: 18 | dart format --set-exit-if-changed ./ 19 | 20 | analyzeDart: 21 | dart analyze . 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Package on pub.dev][pubdev_badge]][pubdev_link] 2 | 3 | # PostHog Flutter 4 | 5 | Please see the main [PostHog docs](https://posthog.com/docs). 6 | 7 | Specifically, the [Flutter docs](https://posthog.com/docs/libraries/flutter) details. 8 | 9 | ## Questions? 10 | 11 | ### [Check out our community page.](https://posthog.com/posts) 12 | 13 | [pubdev_badge]: https://img.shields.io/pub/v/posthog_flutter 14 | [pubdev_link]: https://pub.dev/packages/posthog_flutter 15 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | Releasing 2 | ========= 3 | 4 | 1. Update the CHANGELOG.md with the version 5 | 2. Choose a tag name (e.g. `3.0.0`), this is the version number of the release. 6 | 1. Preview releases follow the pattern `3.0.0-alpha.1`, `3.0.0-beta.1`, `3.0.0-RC.1` 7 | 2. Execute the script with the tag's name, the script will update the version file and create a tag. 8 | 9 | ```bash 10 | ./scripts/prepare-release.sh 3.0.0 11 | ``` 12 | 3. Go to [GH Releases](https://github.com/PostHog/posthog-flutter/releases) 13 | 4. Choose a tag name (e.g. `3.0.0`), same as step 2. 14 | 5. Choose a release name (e.g. `3.0.0`), ideally it matches the above. 15 | 6. Write a description of the release. 16 | 7. Publish the release. 17 | 8. GH Action (publish.yml) is doing everything else [automatically](https://pub.dev/packages/posthog_flutter/admin). 18 | 9. Done. 19 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | include: package:flutter_lints/flutter.yaml 2 | 3 | # Additional information about this file can be found at 4 | # https://dart.dev/guides/language/analysis-options 5 | -------------------------------------------------------------------------------- /android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .cxx 10 | 11 | /.settings 12 | .project 13 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.posthog.flutter' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.8.10' 6 | repositories { 7 | google() 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | classpath 'com.android.tools.build:gradle:7.4.2' 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | mavenCentral() 21 | } 22 | } 23 | 24 | apply plugin: 'com.android.library' 25 | apply plugin: 'kotlin-android' 26 | 27 | android { 28 | if (project.android.hasProperty("namespace")) { 29 | namespace 'com.posthog.flutter' 30 | } 31 | 32 | compileSdkVersion 33 33 | 34 | compileOptions { 35 | sourceCompatibility JavaVersion.VERSION_1_8 36 | targetCompatibility JavaVersion.VERSION_1_8 37 | } 38 | 39 | kotlinOptions { 40 | jvmTarget = '1.8' 41 | languageVersion = "1.6" 42 | apiVersion = "1.6" 43 | } 44 | 45 | sourceSets { 46 | main.java.srcDirs += 'src/main/kotlin' 47 | test.java.srcDirs += 'src/test/kotlin' 48 | } 49 | 50 | defaultConfig { 51 | minSdkVersion 21 52 | } 53 | 54 | dependencies { 55 | testImplementation 'org.jetbrains.kotlin:kotlin-test' 56 | testImplementation 'org.mockito:mockito-core:5.0.0' 57 | // + Version 3.+ and the versions up to 4.0, not including 4.0 and higher 58 | implementation 'com.posthog:posthog-android:3.+' 59 | } 60 | 61 | testOptions { 62 | unitTests.all { 63 | useJUnitPlatform() 64 | 65 | testLogging { 66 | events "passed", "skipped", "failed", "standardOut", "standardError" 67 | outputs.upToDateWhen { false } 68 | showStandardStreams = true 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx4G 2 | -------------------------------------------------------------------------------- /android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 16 15:21:19 CEST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-bin.zip 7 | -------------------------------------------------------------------------------- /android/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'posthog_flutter' 2 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /android/src/main/kotlin/com/posthog/flutter/PostHogVersion.kt: -------------------------------------------------------------------------------- 1 | package com.posthog.flutter 2 | 3 | internal val postHogVersion = "5.0.0" 4 | -------------------------------------------------------------------------------- /android/src/main/kotlin/com/posthog/flutter/SnapshotSender.kt: -------------------------------------------------------------------------------- 1 | package com.posthog.flutter 2 | 3 | import android.graphics.BitmapFactory 4 | import com.posthog.android.internal.base64 5 | import com.posthog.internal.replay.RREvent 6 | import com.posthog.internal.replay.RRFullSnapshotEvent 7 | import com.posthog.internal.replay.RRMetaEvent 8 | import com.posthog.internal.replay.RRStyle 9 | import com.posthog.internal.replay.RRWireframe 10 | import com.posthog.internal.replay.capture 11 | 12 | class SnapshotSender { 13 | fun sendFullSnapshot( 14 | imageBytes: ByteArray, 15 | id: Int, 16 | x: Int, 17 | y: Int, 18 | ) { 19 | val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size) 20 | val base64String = bitmap.base64() 21 | 22 | val wireframe = 23 | RRWireframe( 24 | id = id, 25 | x = x, 26 | y = y, 27 | width = bitmap.width, 28 | height = bitmap.height, 29 | type = "screenshot", 30 | base64 = base64String, 31 | style = RRStyle(), 32 | ) 33 | 34 | val snapshotEvent = 35 | RRFullSnapshotEvent( 36 | listOf(wireframe), 37 | initialOffsetTop = 0, 38 | initialOffsetLeft = 0, 39 | timestamp = System.currentTimeMillis(), 40 | ) 41 | 42 | listOf(snapshotEvent).capture() 43 | } 44 | 45 | fun sendMetaEvent( 46 | width: Int, 47 | height: Int, 48 | screen: String, 49 | ) { 50 | val metaEvent = 51 | RRMetaEvent( 52 | href = screen, 53 | width = width, 54 | height = height, 55 | timestamp = System.currentTimeMillis(), 56 | ) 57 | 58 | val events = mutableListOf() 59 | events.add(metaEvent) 60 | 61 | events.capture() 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /android/src/test/kotlin/com/posthog/flutter/PosthogFlutterPluginTest.kt: -------------------------------------------------------------------------------- 1 | package com.posthog.flutter 2 | 3 | import io.flutter.plugin.common.MethodCall 4 | import io.flutter.plugin.common.MethodChannel 5 | import org.mockito.Mockito 6 | import kotlin.test.Test 7 | 8 | /* 9 | * This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation. 10 | * 11 | * Once you have built the plugin's example app, you can run these tests from the command 12 | * line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or 13 | * you can run them directly from IDEs that support JUnit such as Android Studio. 14 | */ 15 | 16 | internal class PosthogFlutterPluginTest { 17 | @Test 18 | fun onMethodCall_identify_returnsExpectedValue() { 19 | val plugin = PosthogFlutterPlugin() 20 | 21 | var arguments = mapOf("userId" to "abc") 22 | 23 | val call = MethodCall("identify", arguments) 24 | val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java) 25 | plugin.onMethodCall(call, mockResult) 26 | 27 | Mockito.verify(mockResult).success(true) 28 | } 29 | 30 | @Test 31 | fun onMethodCall_alias_returnsExpectedValue() { 32 | val plugin = PosthogFlutterPlugin() 33 | 34 | var arguments = mapOf("alias" to "abc") 35 | 36 | val call = MethodCall("alias", arguments) 37 | val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java) 38 | plugin.onMethodCall(call, mockResult) 39 | 40 | Mockito.verify(mockResult).success(true) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /dangerfile.js: -------------------------------------------------------------------------------- 1 | async function checkChangelog() { 2 | const changelogFile = "CHANGELOG.md"; 3 | 4 | // Check if skipped 5 | const skipChangelog = 6 | danger.github && (danger.github.pr.body + "").includes("#skip-changelog"); 7 | 8 | if (skipChangelog) { 9 | return; 10 | } 11 | 12 | // Check if current PR has an entry in changelog 13 | const changelogContents = await danger.github.utils.fileContents( 14 | changelogFile 15 | ); 16 | 17 | const hasChangelogEntry = RegExp(`#${danger.github.pr.number}\\b`).test( 18 | changelogContents 19 | ); 20 | 21 | if (hasChangelogEntry) { 22 | return; 23 | } 24 | 25 | // Report missing changelog entry 26 | fail( 27 | "Please consider adding a changelog entry for the next release.", 28 | changelogFile 29 | ); 30 | 31 | const prTitleFormatted = danger.github.pr.title 32 | .split(": ") 33 | .slice(-1)[0] 34 | .trim() 35 | .replace(/\.+$/, ""); 36 | 37 | markdown( 38 | ` 39 | ### Instructions and example for changelog 40 | Please add an entry to \`CHANGELOG.md\` to the "Next" section. Make sure the entry includes this PR's number. 41 | Example: 42 | \`\`\`markdown 43 | ## Next 44 | - ${prTitleFormatted} ([#${danger.github.pr.number}](${danger.github.pr.html_url})) 45 | \`\`\` 46 | If none of the above apply, you can opt out of this check by adding \`#skip-changelog\` to the PR description.`.trim() 47 | ); 48 | } 49 | 50 | async function checkAll() { 51 | const isDraft = danger.github.pr.mergeable_state === "draft"; 52 | 53 | if (isDraft) { 54 | return; 55 | } 56 | 57 | await checkChangelog(); 58 | } 59 | 60 | schedule(checkAll); 61 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: "78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9" 8 | channel: "stable" 9 | 10 | project_type: app 11 | 12 | # Tracks metadata for the flutter migrate command 13 | migration: 14 | platforms: 15 | - platform: root 16 | create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9 17 | base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9 18 | - platform: macos 19 | create_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9 20 | base_revision: 78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9 21 | 22 | # User provided section 23 | 24 | # List of Local paths (relative to this file) that should be 25 | # ignored by the migrate tool. 26 | # 27 | # Files that are not part of the templates will be ignored by default. 28 | unmanaged_files: 29 | - 'lib/main.dart' 30 | - 'ios/Runner.xcodeproj/project.pbxproj' 31 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # posthog_flutter_example 2 | 3 | Demonstrates how to use the posthog_flutter plugin. 4 | 5 | ## Getting Started 6 | 7 | This project is a starting point for a Flutter application. 8 | 9 | A few resources to get you started if this is your first Flutter project: 10 | 11 | - [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) 12 | - [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) 13 | 14 | For help getting started with Flutter development, view the 15 | [online documentation](https://docs.flutter.dev/), which offers tutorials, 16 | samples, guidance on mobile development, and a full API reference. 17 | -------------------------------------------------------------------------------- /example/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # This file configures the analyzer, which statically analyzes Dart code to 2 | # check for errors, warnings, and lints. 3 | # 4 | # The issues identified by the analyzer are surfaced in the UI of Dart-enabled 5 | # IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be 6 | # invoked from the command line by running `flutter analyze`. 7 | 8 | # The following line activates a set of recommended lints for Flutter apps, 9 | # packages, and plugins designed to encourage good coding practices. 10 | include: package:flutter_lints/flutter.yaml 11 | 12 | linter: 13 | # The lint rules applied to this project can be customized in the 14 | # section below to disable rules from the `package:flutter_lints/flutter.yaml` 15 | # included above or to enable additional rules. A list of all available lints 16 | # and their documentation is published at https://dart.dev/lints. 17 | # 18 | # Instead of disabling a lint rule for the entire project in the 19 | # section below, it can also be suppressed for a single line of code 20 | # or a specific dart file by using the `// ignore: name_of_lint` and 21 | # `// ignore_for_file: name_of_lint` syntax on the line or in the file 22 | # producing the lint. 23 | rules: 24 | # avoid_print: false # Uncomment to disable the `avoid_print` rule 25 | # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule 26 | 27 | # Additional information about this file can be found at 28 | # https://dart.dev/guides/language/analysis-options 29 | -------------------------------------------------------------------------------- /example/android/.gitignore: -------------------------------------------------------------------------------- 1 | gradle-wrapper.jar 2 | /.gradle 3 | /captures/ 4 | /gradlew 5 | /gradlew.bat 6 | /local.properties 7 | GeneratedPluginRegistrant.java 8 | 9 | # Remember to never publicly share your keystore. 10 | # See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app 11 | key.properties 12 | **/*.keystore 13 | **/*.jks 14 | -------------------------------------------------------------------------------- /example/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "com.android.application" 3 | id "kotlin-android" 4 | id "dev.flutter.flutter-gradle-plugin" 5 | } 6 | 7 | def localProperties = new Properties() 8 | def localPropertiesFile = rootProject.file('local.properties') 9 | if (localPropertiesFile.exists()) { 10 | localPropertiesFile.withReader('UTF-8') { reader -> 11 | localProperties.load(reader) 12 | } 13 | } 14 | 15 | def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 16 | if (flutterVersionCode == null) { 17 | flutterVersionCode = '1' 18 | } 19 | 20 | def flutterVersionName = localProperties.getProperty('flutter.versionName') 21 | if (flutterVersionName == null) { 22 | flutterVersionName = '1.0' 23 | } 24 | 25 | android { 26 | namespace "com.example.flutter" 27 | // use flutter.compileSdkVersion once https://github.com/flutter/flutter/issues/153893 is fixed 28 | compileSdkVersion 34 29 | ndkVersion flutter.ndkVersion 30 | 31 | compileOptions { 32 | sourceCompatibility JavaVersion.VERSION_1_8 33 | targetCompatibility JavaVersion.VERSION_1_8 34 | } 35 | 36 | kotlinOptions { 37 | jvmTarget = '1.8' 38 | } 39 | 40 | sourceSets { 41 | main.java.srcDirs += 'src/main/kotlin' 42 | } 43 | 44 | defaultConfig { 45 | applicationId "com.example.flutter" 46 | // You can update the following values to match your application needs. 47 | // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. 48 | minSdkVersion 21 49 | targetSdkVersion flutter.targetSdkVersion 50 | versionCode flutterVersionCode.toInteger() 51 | versionName flutterVersionName 52 | } 53 | 54 | buildTypes { 55 | release { 56 | minifyEnabled true 57 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 58 | shrinkResources true 59 | // Signing with the debug keys for now, so `flutter run --release` works. 60 | signingConfig signingConfigs.debug 61 | } 62 | } 63 | } 64 | 65 | flutter { 66 | source '../..' 67 | } 68 | 69 | dependencies {} 70 | -------------------------------------------------------------------------------- /example/android/app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 15 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 29 | 32 | 33 | 37 | 40 | 43 | 46 | 49 | 50 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /example/android/app/src/main/kotlin/com/example/flutter/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.flutter 2 | 3 | import io.flutter.embedding.android.FlutterActivity 4 | 5 | class MainActivity : FlutterActivity() 6 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable-v21/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/values-night/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/android/app/src/profile/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.8.10' 3 | repositories { 4 | google() 5 | mavenCentral() 6 | } 7 | 8 | dependencies { 9 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | google() 16 | mavenCentral() 17 | } 18 | } 19 | 20 | rootProject.buildDir = '../build' 21 | subprojects { 22 | project.buildDir = "${rootProject.buildDir}/${project.name}" 23 | } 24 | subprojects { 25 | project.evaluationDependsOn(':app') 26 | } 27 | 28 | tasks.register("clean", Delete) { 29 | delete rootProject.buildDir 30 | } 31 | -------------------------------------------------------------------------------- /example/android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx4G 2 | android.useAndroidX=true 3 | android.suppressUnsupportedCompileSdk=34 4 | -------------------------------------------------------------------------------- /example/android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /example/android/settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | def flutterSdkPath = { 3 | def properties = new Properties() 4 | file("local.properties").withInputStream { properties.load(it) } 5 | def flutterSdkPath = properties.getProperty("flutter.sdk") 6 | assert flutterSdkPath != null, "flutter.sdk not set in local.properties" 7 | return flutterSdkPath 8 | } 9 | settings.ext.flutterSdkPath = flutterSdkPath() 10 | 11 | includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle") 12 | 13 | repositories { 14 | google() 15 | mavenCentral() 16 | gradlePluginPortal() 17 | } 18 | 19 | plugins { 20 | id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false 21 | } 22 | } 23 | 24 | plugins { 25 | id "dev.flutter.flutter-plugin-loader" version "1.0.0" 26 | id "com.android.application" version "7.4.2" apply false 27 | } 28 | 29 | include ":app" 30 | -------------------------------------------------------------------------------- /example/assets/posthog_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/assets/posthog_logo.png -------------------------------------------------------------------------------- /example/assets/training_posthog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/assets/training_posthog.png -------------------------------------------------------------------------------- /example/ios/.gitignore: -------------------------------------------------------------------------------- 1 | **/dgph 2 | *.mode1v3 3 | *.mode2v3 4 | *.moved-aside 5 | *.pbxuser 6 | *.perspectivev3 7 | **/*sync/ 8 | .sconsign.dblite 9 | .tags* 10 | **/.vagrant/ 11 | **/DerivedData/ 12 | Icon? 13 | **/Pods/ 14 | **/.symlinks/ 15 | profile 16 | xcuserdata 17 | **/.generated/ 18 | Flutter/App.framework 19 | Flutter/Flutter.framework 20 | Flutter/Flutter.podspec 21 | Flutter/Generated.xcconfig 22 | Flutter/ephemeral/ 23 | Flutter/app.flx 24 | Flutter/app.zip 25 | Flutter/flutter_assets/ 26 | Flutter/flutter_export_environment.sh 27 | ServiceDefinitions.json 28 | Runner/GeneratedPluginRegistrant.* 29 | 30 | # Exceptions to above rules. 31 | !default.mode1v3 32 | !default.mode2v3 33 | !default.pbxuser 34 | !default.perspectivev3 35 | -------------------------------------------------------------------------------- /example/ios/Flutter/AppFrameworkInfo.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | App 9 | CFBundleIdentifier 10 | io.flutter.flutter.app 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | App 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1.0 23 | MinimumOSVersion 24 | 13.0 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Flutter/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/ios/Flutter/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/ios/Podfile: -------------------------------------------------------------------------------- 1 | # Uncomment this line to define a global platform for your project 2 | platform :ios, '13.0' 3 | 4 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 5 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 6 | 7 | project 'Runner', { 8 | 'Debug' => :debug, 9 | 'Profile' => :release, 10 | 'Release' => :release, 11 | } 12 | 13 | def flutter_root 14 | generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) 15 | unless File.exist?(generated_xcode_build_settings_path) 16 | raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" 17 | end 18 | 19 | File.foreach(generated_xcode_build_settings_path) do |line| 20 | matches = line.match(/FLUTTER_ROOT\=(.*)/) 21 | return matches[1].strip if matches 22 | end 23 | raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" 24 | end 25 | 26 | require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 27 | 28 | flutter_ios_podfile_setup 29 | 30 | target 'Runner' do 31 | use_frameworks! 32 | use_modular_headers! 33 | 34 | flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) 35 | target 'RunnerTests' do 36 | inherit! :search_paths 37 | end 38 | end 39 | 40 | post_install do |installer| 41 | installer.pods_project.targets.each do |target| 42 | flutter_additional_ios_build_settings(target) 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 37 | 38 | 39 | 40 | 43 | 49 | 50 | 51 | 52 | 53 | 64 | 66 | 72 | 73 | 74 | 75 | 81 | 83 | 89 | 90 | 91 | 92 | 94 | 95 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | 4 | @interface AppDelegate : FlutterAppDelegate 5 | 6 | @end 7 | -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.m: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "GeneratedPluginRegistrant.h" 3 | 4 | @implementation AppDelegate 5 | 6 | - (BOOL)application:(UIApplication *)application 7 | didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 8 | [GeneratedPluginRegistrant registerWithRegistry:self]; 9 | // Override point for customization after application launch. 10 | return [super application:application didFinishLaunchingWithOptions:launchOptions]; 11 | } 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Flutter 3 | 4 | @main 5 | @objc class AppDelegate: FlutterAppDelegate { 6 | override func application( 7 | _ application: UIApplication, 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? 9 | ) -> Bool { 10 | GeneratedPluginRegistrant.register(with: self) 11 | return super.application(application, didFinishLaunchingWithOptions: launchOptions) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "Icon-App-20x20@2x.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "Icon-App-20x20@3x.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "Icon-App-29x29@1x.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "Icon-App-29x29@2x.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "Icon-App-29x29@3x.png", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "Icon-App-40x40@2x.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "Icon-App-40x40@3x.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "size" : "60x60", 47 | "idiom" : "iphone", 48 | "filename" : "Icon-App-60x60@2x.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "60x60", 53 | "idiom" : "iphone", 54 | "filename" : "Icon-App-60x60@3x.png", 55 | "scale" : "3x" 56 | }, 57 | { 58 | "size" : "20x20", 59 | "idiom" : "ipad", 60 | "filename" : "Icon-App-20x20@1x.png", 61 | "scale" : "1x" 62 | }, 63 | { 64 | "size" : "20x20", 65 | "idiom" : "ipad", 66 | "filename" : "Icon-App-20x20@2x.png", 67 | "scale" : "2x" 68 | }, 69 | { 70 | "size" : "29x29", 71 | "idiom" : "ipad", 72 | "filename" : "Icon-App-29x29@1x.png", 73 | "scale" : "1x" 74 | }, 75 | { 76 | "size" : "29x29", 77 | "idiom" : "ipad", 78 | "filename" : "Icon-App-29x29@2x.png", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "size" : "40x40", 83 | "idiom" : "ipad", 84 | "filename" : "Icon-App-40x40@1x.png", 85 | "scale" : "1x" 86 | }, 87 | { 88 | "size" : "40x40", 89 | "idiom" : "ipad", 90 | "filename" : "Icon-App-40x40@2x.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "76x76", 95 | "idiom" : "ipad", 96 | "filename" : "Icon-App-76x76@1x.png", 97 | "scale" : "1x" 98 | }, 99 | { 100 | "size" : "76x76", 101 | "idiom" : "ipad", 102 | "filename" : "Icon-App-76x76@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "83.5x83.5", 107 | "idiom" : "ipad", 108 | "filename" : "Icon-App-83.5x83.5@2x.png", 109 | "scale" : "2x" 110 | }, 111 | { 112 | "size" : "1024x1024", 113 | "idiom" : "ios-marketing", 114 | "filename" : "Icon-App-1024x1024@1x.png", 115 | "scale" : "1x" 116 | } 117 | ], 118 | "info" : { 119 | "version" : 1, 120 | "author" : "xcode" 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "LaunchImage.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "LaunchImage@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "LaunchImage@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md: -------------------------------------------------------------------------------- 1 | # Launch Screen Assets 2 | 3 | You can customize the launch screen with your own desired assets by replacing the image files in this directory. 4 | 5 | You can also do it by opening your Flutter project's Xcode project with `open ios/Runner.xcworkspace`, selecting `Runner/Assets.xcassets` in the Project Navigator and dropping in the desired images. -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleDisplayName 8 | Posthog Flutter 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | posthog_flutter_example 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | $(FLUTTER_BUILD_NAME) 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | $(FLUTTER_BUILD_NUMBER) 25 | LSRequiresIPhoneOS 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | com.posthog.posthog.POSTHOG_HOST 45 | https://us.i.posthog.com 46 | com.posthog.posthog.API_KEY 47 | phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D 48 | com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS 49 | 50 | com.posthog.posthog.DEBUG 51 | 52 | CADisableMinimumFrameDurationOnPhone 53 | 54 | UIApplicationSupportsIndirectInputEvents 55 | 56 | com.posthog.posthog.AUTO_INIT 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /example/ios/Runner/Runner-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | #import "GeneratedPluginRegistrant.h" 2 | -------------------------------------------------------------------------------- /example/ios/Runner/main.m: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | #import "AppDelegate.h" 4 | 5 | int main(int argc, char* argv[]) { 6 | @autoreleasepool { 7 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /example/ios/RunnerTests/RunnerTests.swift: -------------------------------------------------------------------------------- 1 | import Flutter 2 | import UIKit 3 | import XCTest 4 | 5 | @testable import posthog_flutter 6 | 7 | // This demonstrates a simple unit test of the Swift portion of this plugin's implementation. 8 | // 9 | // See https://developer.apple.com/documentation/xctest for more information about using XCTest. 10 | 11 | class RunnerTests: XCTestCase { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | import 'package:posthog_flutter/posthog_flutter.dart'; 4 | 5 | Future main() async { 6 | // // init WidgetsFlutterBinding if not yet 7 | 8 | WidgetsFlutterBinding.ensureInitialized(); 9 | final config = 10 | PostHogConfig('phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D'); 11 | config.debug = true; 12 | config.captureApplicationLifecycleEvents = false; 13 | config.host = 'https://us.i.posthog.com'; 14 | config.sessionReplay = true; 15 | config.sessionReplayConfig.maskAllTexts = false; 16 | config.sessionReplayConfig.maskAllImages = false; 17 | config.sessionReplayConfig.throttleDelay = const Duration(milliseconds: 1000); 18 | config.flushAt = 1; 19 | await Posthog().setup(config); 20 | 21 | runApp(const MyApp()); 22 | } 23 | 24 | class MyApp extends StatefulWidget { 25 | const MyApp({super.key}); 26 | 27 | @override 28 | State createState() => _MyAppState(); 29 | } 30 | 31 | class _MyAppState extends State { 32 | @override 33 | void initState() { 34 | super.initState(); 35 | } 36 | 37 | @override 38 | Widget build(BuildContext context) { 39 | return PostHogWidget( 40 | child: MaterialApp( 41 | navigatorObservers: [PosthogObserver()], 42 | title: 'Flutter App', 43 | home: const _InitialScreen(), 44 | ), 45 | ); 46 | } 47 | } 48 | 49 | class _InitialScreen extends StatefulWidget { 50 | const _InitialScreen({Key? key}) : super(key: key); 51 | 52 | @override 53 | _InitialScreenState createState() => _InitialScreenState(); 54 | } 55 | 56 | class _InitialScreenState extends State<_InitialScreen> { 57 | final _posthogFlutterPlugin = Posthog(); 58 | dynamic _result = ""; 59 | 60 | @override 61 | void initState() { 62 | super.initState(); 63 | } 64 | 65 | @override 66 | Widget build(BuildContext context) { 67 | return Scaffold( 68 | appBar: AppBar( 69 | title: const Text('PostHog Flutter App'), 70 | ), 71 | body: SingleChildScrollView( 72 | child: Padding( 73 | padding: const EdgeInsets.all(16), 74 | child: Center( 75 | child: Column( 76 | children: [ 77 | ElevatedButton( 78 | onPressed: () { 79 | Navigator.push( 80 | context, 81 | MaterialPageRoute( 82 | builder: (context) => const _SecondRoute(), 83 | settings: const RouteSettings(name: 'second_route')), 84 | ); 85 | }, 86 | child: const PostHogMaskWidget( 87 | child: Text( 88 | 'Go to Second Route', 89 | ), 90 | ), 91 | ), 92 | const Padding( 93 | padding: EdgeInsets.all(8.0), 94 | child: Text( 95 | "Capture", 96 | style: TextStyle(fontWeight: FontWeight.bold), 97 | ), 98 | ), 99 | Row( 100 | mainAxisAlignment: MainAxisAlignment.spaceEvenly, 101 | children: [ 102 | ElevatedButton( 103 | onPressed: () { 104 | _posthogFlutterPlugin 105 | .screen(screenName: "my screen", properties: { 106 | "foo": "bar", 107 | }); 108 | }, 109 | child: const Text("Capture Screen manually"), 110 | ), 111 | ElevatedButton( 112 | onPressed: () { 113 | _posthogFlutterPlugin 114 | .capture(eventName: "eventName", properties: { 115 | "foo": "bar", 116 | }); 117 | }, 118 | child: const Text("Capture Event"), 119 | ), 120 | ], 121 | ), 122 | const Divider(), 123 | const Padding( 124 | padding: EdgeInsets.all(8.0), 125 | child: Text( 126 | "Activity", 127 | style: TextStyle(fontWeight: FontWeight.bold), 128 | ), 129 | ), 130 | Row( 131 | mainAxisAlignment: MainAxisAlignment.spaceEvenly, 132 | children: [ 133 | ElevatedButton( 134 | style: ElevatedButton.styleFrom( 135 | backgroundColor: Colors.red, 136 | ), 137 | onPressed: () { 138 | _posthogFlutterPlugin.disable(); 139 | }, 140 | child: const Text("Disable Capture"), 141 | ), 142 | ElevatedButton( 143 | style: ElevatedButton.styleFrom( 144 | backgroundColor: Colors.green, 145 | ), 146 | onPressed: () { 147 | _posthogFlutterPlugin.enable(); 148 | }, 149 | child: const Text("Enable Capture"), 150 | ), 151 | ], 152 | ), 153 | ElevatedButton( 154 | onPressed: () async { 155 | await _posthogFlutterPlugin.register("foo", "bar"); 156 | }, 157 | child: const Text("Register"), 158 | ), 159 | ElevatedButton( 160 | onPressed: () async { 161 | await _posthogFlutterPlugin.unregister("foo"); 162 | }, 163 | child: const Text("Unregister"), 164 | ), 165 | ElevatedButton( 166 | onPressed: () async { 167 | await _posthogFlutterPlugin.group( 168 | groupType: "theType", 169 | groupKey: "theKey", 170 | groupProperties: { 171 | "foo": "bar", 172 | }); 173 | }, 174 | child: const Text("Group"), 175 | ), 176 | ElevatedButton( 177 | onPressed: () async { 178 | await _posthogFlutterPlugin 179 | .identify(userId: "myId", userProperties: { 180 | "foo": "bar", 181 | }, userPropertiesSetOnce: { 182 | "foo1": "bar1", 183 | }); 184 | }, 185 | child: const Text("Identify"), 186 | ), 187 | ElevatedButton( 188 | onPressed: () async { 189 | await _posthogFlutterPlugin.alias(alias: "myAlias"); 190 | }, 191 | child: const Text("Alias"), 192 | ), 193 | ElevatedButton( 194 | onPressed: () async { 195 | await _posthogFlutterPlugin.debug(true); 196 | }, 197 | child: const Text("Debug"), 198 | ), 199 | ElevatedButton( 200 | onPressed: () async { 201 | await _posthogFlutterPlugin.reset(); 202 | }, 203 | child: const Text("Reset"), 204 | ), 205 | ElevatedButton( 206 | onPressed: () async { 207 | await _posthogFlutterPlugin.flush(); 208 | }, 209 | child: const Text("Flush"), 210 | ), 211 | ElevatedButton( 212 | onPressed: () async { 213 | final result = 214 | await _posthogFlutterPlugin.getDistinctId(); 215 | setState(() { 216 | _result = result; 217 | }); 218 | }, 219 | child: const PostHogMaskWidget( 220 | child: Text("distinctId"), 221 | )), 222 | const Divider(), 223 | const Padding( 224 | padding: EdgeInsets.all(8.0), 225 | child: Text( 226 | "Feature flags", 227 | style: TextStyle(fontWeight: FontWeight.bold), 228 | ), 229 | ), 230 | ElevatedButton( 231 | onPressed: () async { 232 | final result = await _posthogFlutterPlugin 233 | .getFeatureFlag("feature_name"); 234 | setState(() { 235 | _result = result; 236 | }); 237 | }, 238 | child: const Text("Get Feature Flag status"), 239 | ), 240 | ElevatedButton( 241 | onPressed: () async { 242 | final result = await _posthogFlutterPlugin 243 | .isFeatureEnabled("feature_name"); 244 | setState(() { 245 | _result = result; 246 | }); 247 | }, 248 | child: const Text("isFeatureEnabled"), 249 | ), 250 | ElevatedButton( 251 | onPressed: () async { 252 | final result = await _posthogFlutterPlugin 253 | .getFeatureFlagPayload("feature_name"); 254 | setState(() { 255 | _result = result; 256 | }); 257 | }, 258 | child: const Text("getFeatureFlagPayload"), 259 | ), 260 | ElevatedButton( 261 | onPressed: () async { 262 | await _posthogFlutterPlugin.reloadFeatureFlags(); 263 | }, 264 | child: const PostHogMaskWidget( 265 | child: Text("reloadFeatureFlags")), 266 | ), 267 | const Divider(), 268 | const Padding( 269 | padding: EdgeInsets.all(8.0), 270 | child: Text( 271 | "Data result", 272 | style: TextStyle(fontWeight: FontWeight.bold), 273 | ), 274 | ), 275 | Text(_result.toString()), 276 | ], 277 | ), 278 | ), 279 | ), 280 | ), 281 | ); 282 | } 283 | } 284 | 285 | class _SecondRoute extends StatefulWidget { 286 | const _SecondRoute(); 287 | 288 | @override 289 | _SecondRouteState createState() => _SecondRouteState(); 290 | } 291 | 292 | class _SecondRouteState extends State<_SecondRoute> 293 | with WidgetsBindingObserver { 294 | @override 295 | void initState() { 296 | super.initState(); 297 | } 298 | 299 | @override 300 | Widget build(BuildContext context) { 301 | return Scaffold( 302 | appBar: AppBar( 303 | title: const PostHogMaskWidget(child: Text('First Route')), 304 | ), 305 | body: Center( 306 | child: RepaintBoundary( 307 | child: Column( 308 | mainAxisAlignment: MainAxisAlignment.center, 309 | children: [ 310 | ElevatedButton( 311 | child: const PostHogMaskWidget(child: Text('Open route')), 312 | onPressed: () { 313 | Navigator.push( 314 | context, 315 | MaterialPageRoute( 316 | builder: (context) => const ThirdRoute(), 317 | settings: const RouteSettings(name: 'third_route'), 318 | ), 319 | ).then((_) {}); 320 | }, 321 | ), 322 | const SizedBox(height: 20), 323 | const PostHogMaskWidget( 324 | child: TextField( 325 | decoration: InputDecoration( 326 | labelText: 'Sensitive Text Input', 327 | hintText: 'Enter sensitive data', 328 | border: OutlineInputBorder(), 329 | ), 330 | )), 331 | const SizedBox(height: 20), 332 | PostHogMaskWidget( 333 | child: Image.asset( 334 | 'assets/training_posthog.png', 335 | height: 200, 336 | )), 337 | const SizedBox(height: 20), 338 | ], 339 | ), 340 | ), 341 | ), 342 | ); 343 | } 344 | } 345 | 346 | class ThirdRoute extends StatelessWidget { 347 | const ThirdRoute({super.key}); 348 | 349 | @override 350 | Widget build(BuildContext context) { 351 | return Scaffold( 352 | appBar: AppBar( 353 | title: const Text('Third Route'), 354 | ), 355 | body: Padding( 356 | padding: const EdgeInsets.all(8.0), 357 | child: GridView.builder( 358 | gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( 359 | crossAxisCount: 4, 360 | crossAxisSpacing: 10.0, 361 | mainAxisSpacing: 10.0, 362 | ), 363 | itemCount: 16, 364 | itemBuilder: (context, index) { 365 | return Image.asset( 366 | 'assets/posthog_logo.png', 367 | fit: BoxFit.cover, 368 | ); 369 | }, 370 | ), 371 | ), 372 | ); 373 | } 374 | } 375 | -------------------------------------------------------------------------------- /example/macos/.gitignore: -------------------------------------------------------------------------------- 1 | # Flutter-related 2 | **/Flutter/ephemeral/ 3 | **/Pods/ 4 | 5 | # Xcode-related 6 | **/dgph 7 | **/xcuserdata/ 8 | -------------------------------------------------------------------------------- /example/macos/Flutter/Flutter-Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "ephemeral/Flutter-Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Flutter/Flutter-Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "ephemeral/Flutter-Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Flutter/GeneratedPluginRegistrant.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | import FlutterMacOS 6 | import Foundation 7 | 8 | import posthog_flutter 9 | 10 | func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { 11 | PosthogFlutterPlugin.register(with: registry.registrar(forPlugin: "PosthogFlutterPlugin")) 12 | } 13 | -------------------------------------------------------------------------------- /example/macos/Podfile: -------------------------------------------------------------------------------- 1 | platform :osx, '10.15' 2 | 3 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 4 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 5 | 6 | project 'Runner', { 7 | 'Debug' => :debug, 8 | 'Profile' => :release, 9 | 'Release' => :release, 10 | } 11 | 12 | def flutter_root 13 | generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__) 14 | unless File.exist?(generated_xcode_build_settings_path) 15 | raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first" 16 | end 17 | 18 | File.foreach(generated_xcode_build_settings_path) do |line| 19 | matches = line.match(/FLUTTER_ROOT\=(.*)/) 20 | return matches[1].strip if matches 21 | end 22 | raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\"" 23 | end 24 | 25 | require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 26 | 27 | flutter_macos_podfile_setup 28 | 29 | target 'Runner' do 30 | use_frameworks! 31 | use_modular_headers! 32 | 33 | flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__)) 34 | target 'RunnerTests' do 35 | inherit! :search_paths 36 | end 37 | end 38 | 39 | post_install do |installer| 40 | installer.pods_project.targets.each do |target| 41 | flutter_additional_macos_build_settings(target) 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /example/macos/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 37 | 38 | 39 | 40 | 43 | 49 | 50 | 51 | 52 | 53 | 63 | 65 | 71 | 72 | 73 | 74 | 80 | 82 | 88 | 89 | 90 | 91 | 93 | 94 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /example/macos/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/macos/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/macos/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | @NSApplicationMain 5 | class AppDelegate: FlutterAppDelegate { 6 | override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { 7 | return true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "16x16", 5 | "idiom" : "mac", 6 | "filename" : "app_icon_16.png", 7 | "scale" : "1x" 8 | }, 9 | { 10 | "size" : "16x16", 11 | "idiom" : "mac", 12 | "filename" : "app_icon_32.png", 13 | "scale" : "2x" 14 | }, 15 | { 16 | "size" : "32x32", 17 | "idiom" : "mac", 18 | "filename" : "app_icon_32.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "32x32", 23 | "idiom" : "mac", 24 | "filename" : "app_icon_64.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "128x128", 29 | "idiom" : "mac", 30 | "filename" : "app_icon_128.png", 31 | "scale" : "1x" 32 | }, 33 | { 34 | "size" : "128x128", 35 | "idiom" : "mac", 36 | "filename" : "app_icon_256.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "256x256", 41 | "idiom" : "mac", 42 | "filename" : "app_icon_256.png", 43 | "scale" : "1x" 44 | }, 45 | { 46 | "size" : "256x256", 47 | "idiom" : "mac", 48 | "filename" : "app_icon_512.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "512x512", 53 | "idiom" : "mac", 54 | "filename" : "app_icon_512.png", 55 | "scale" : "1x" 56 | }, 57 | { 58 | "size" : "512x512", 59 | "idiom" : "mac", 60 | "filename" : "app_icon_1024.png", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_1024.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_128.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_16.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_32.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_512.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_64.png -------------------------------------------------------------------------------- /example/macos/Runner/Configs/AppInfo.xcconfig: -------------------------------------------------------------------------------- 1 | // Application-level settings for the Runner target. 2 | // 3 | // This may be replaced with something auto-generated from metadata (e.g., pubspec.yaml) in the 4 | // future. If not, the values below would default to using the project name when this becomes a 5 | // 'flutter create' template. 6 | 7 | // The application's name. By default this is also the title of the Flutter window. 8 | PRODUCT_NAME = example 9 | 10 | // The application's bundle identifier 11 | PRODUCT_BUNDLE_IDENTIFIER = com.example.example 12 | 13 | // The copyright displayed in application information 14 | PRODUCT_COPYRIGHT = Copyright © 2024 com.example. All rights reserved. 15 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include "../../Flutter/Flutter-Debug.xcconfig" 2 | #include "Warnings.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include "../../Flutter/Flutter-Release.xcconfig" 2 | #include "Warnings.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Warnings.xcconfig: -------------------------------------------------------------------------------- 1 | WARNING_CFLAGS = -Wall -Wconditional-uninitialized -Wnullable-to-nonnull-conversion -Wmissing-method-return-type -Woverlength-strings 2 | GCC_WARN_UNDECLARED_SELECTOR = YES 3 | CLANG_UNDEFINED_BEHAVIOR_SANITIZER_NULLABILITY = YES 4 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE 5 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES 6 | CLANG_WARN_PRAGMA_PACK = YES 7 | CLANG_WARN_STRICT_PROTOTYPES = YES 8 | CLANG_WARN_COMMA = YES 9 | GCC_WARN_STRICT_SELECTOR_MATCH = YES 10 | CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK = YES 11 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES 12 | GCC_WARN_SHADOW = YES 13 | CLANG_WARN_UNREACHABLE_CODE = YES 14 | -------------------------------------------------------------------------------- /example/macos/Runner/DebugProfile.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.cs.allow-jit 8 | 9 | com.apple.security.network.client 10 | 11 | com.apple.security.network.server 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /example/macos/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | $(FLUTTER_BUILD_NAME) 21 | CFBundleVersion 22 | $(FLUTTER_BUILD_NUMBER) 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | $(PRODUCT_COPYRIGHT) 27 | NSMainNibFile 28 | MainMenu 29 | NSPrincipalClass 30 | NSApplication 31 | 32 | com.posthog.posthog.POSTHOG_HOST 33 | https://us.i.posthog.com 34 | com.posthog.posthog.API_KEY 35 | phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D 36 | com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS 37 | 38 | com.posthog.posthog.DEBUG 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /example/macos/Runner/MainFlutterWindow.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | class MainFlutterWindow: NSWindow { 5 | override func awakeFromNib() { 6 | let flutterViewController = FlutterViewController() 7 | let windowFrame = self.frame 8 | self.contentViewController = flutterViewController 9 | self.setFrame(windowFrame, display: true) 10 | 11 | RegisterGeneratedPlugins(registry: flutterViewController) 12 | 13 | super.awakeFromNib() 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/macos/Runner/Release.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.network.client 8 | 9 | com.apple.security.network.server 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: posthog_flutter_example 2 | description: Demonstrates how to use the posthog_flutter plugin. 3 | # The following line prevents the package from being accidentally published to 4 | # pub.dev using `flutter pub publish`. This is preferred for private packages. 5 | publish_to: "none" # Remove this line if you wish to publish to pub.dev 6 | version: 1.0.0+1 7 | 8 | environment: 9 | sdk: '>=2.18.0 <4.0.0' 10 | flutter: '>=3.3.0' 11 | 12 | # Dependencies specify other packages that your package needs in order to work. 13 | # To automatically upgrade your package dependencies to the latest versions 14 | # consider running `flutter pub upgrade --major-versions`. Alternatively, 15 | # dependencies can be manually updated by changing the version numbers below to 16 | # the latest version available on pub.dev. To see which dependencies have newer 17 | # versions available, run `flutter pub outdated`. 18 | dependencies: 19 | flutter: 20 | sdk: flutter 21 | 22 | posthog_flutter: 23 | # When depending on this package from a real application you should use: 24 | # posthog_flutter: ^x.y.z 25 | # See https://dart.dev/tools/pub/dependencies#version-constraints 26 | # The example app is bundled with the plugin so we use a path dependency on 27 | # the parent directory to use the current plugin's version. 28 | path: ../ 29 | 30 | # The following adds the Cupertino Icons font to your application. 31 | # Use with the CupertinoIcons class for iOS style icons. 32 | cupertino_icons: ^1.0.3 33 | 34 | dev_dependencies: 35 | 36 | # The "flutter_lints" package below contains a set of recommended lints to 37 | # encourage good coding practices. The lint set provided by the package is 38 | # activated in the `analysis_options.yaml` file located at the root of your 39 | # package. See that file for information about deactivating specific lint 40 | # rules and activating additional ones. 41 | flutter_lints: ^2.0.0 42 | 43 | # For information on the generic Dart part of this file, see the 44 | # following page: https://dart.dev/tools/pub/pubspec 45 | 46 | # The following section is specific to Flutter packages. 47 | flutter: 48 | # The following line ensures that the Material Icons font is 49 | # included with your application, so that you can use the icons in 50 | # the material Icons class. 51 | uses-material-design: true 52 | assets: 53 | - assets/ 54 | 55 | # To add assets to your application, add an assets section, like this: 56 | # assets: 57 | # - images/a_dot_burr.jpeg 58 | # - images/a_dot_ham.jpeg 59 | 60 | # An image asset can refer to one or more resolution-specific "variants", see 61 | # https://flutter.dev/assets-and-images/#resolution-aware 62 | 63 | # For details regarding adding assets from package dependencies, see 64 | # https://flutter.dev/assets-and-images/#from-packages 65 | 66 | # To add custom fonts to your application, add a fonts section here, 67 | # in this "flutter" section. Each entry in this list should have a 68 | # "family" key with the font family name, and a "fonts" key with a 69 | # list giving the asset and other descriptors for the font. For 70 | # example: 71 | # fonts: 72 | # - family: Schyler 73 | # fonts: 74 | # - asset: fonts/Schyler-Regular.ttf 75 | # - asset: fonts/Schyler-Italic.ttf 76 | # style: italic 77 | # - family: Trajan Pro 78 | # fonts: 79 | # - asset: fonts/TrajanPro.ttf 80 | # - asset: fonts/TrajanPro_Bold.ttf 81 | # weight: 700 82 | # 83 | # For details regarding fonts from package dependencies, 84 | # see https://flutter.dev/custom-fonts/#from-packages 85 | -------------------------------------------------------------------------------- /example/web/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/web/favicon.png -------------------------------------------------------------------------------- /example/web/icons/Icon-192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/web/icons/Icon-192.png -------------------------------------------------------------------------------- /example/web/icons/Icon-512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/web/icons/Icon-512.png -------------------------------------------------------------------------------- /example/web/icons/Icon-maskable-192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/web/icons/Icon-maskable-192.png -------------------------------------------------------------------------------- /example/web/icons/Icon-maskable-512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/example/web/icons/Icon-maskable-512.png -------------------------------------------------------------------------------- /example/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | posthog_flutter_example 34 | 35 | 36 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /example/web/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "posthog_flutter_example", 3 | "short_name": "posthog_flutter_example", 4 | "start_url": ".", 5 | "display": "standalone", 6 | "background_color": "#0175C2", 7 | "theme_color": "#0175C2", 8 | "description": "Demonstrates how to use the posthog_flutter plugin.", 9 | "orientation": "portrait-primary", 10 | "prefer_related_applications": false, 11 | "icons": [ 12 | { 13 | "src": "icons/Icon-192.png", 14 | "sizes": "192x192", 15 | "type": "image/png" 16 | }, 17 | { 18 | "src": "icons/Icon-512.png", 19 | "sizes": "512x512", 20 | "type": "image/png" 21 | }, 22 | { 23 | "src": "icons/Icon-maskable-192.png", 24 | "sizes": "192x192", 25 | "type": "image/png", 26 | "purpose": "maskable" 27 | }, 28 | { 29 | "src": "icons/Icon-maskable-512.png", 30 | "sizes": "512x512", 31 | "type": "image/png", 32 | "purpose": "maskable" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /ios/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vagrant/ 3 | .sconsign.dblite 4 | .svn/ 5 | 6 | .DS_Store 7 | *.swp 8 | profile 9 | 10 | DerivedData/ 11 | build/ 12 | GeneratedPluginRegistrant.h 13 | GeneratedPluginRegistrant.m 14 | 15 | .generated/ 16 | 17 | *.pbxuser 18 | *.mode1v3 19 | *.mode2v3 20 | *.perspectivev3 21 | 22 | !default.pbxuser 23 | !default.mode1v3 24 | !default.mode2v3 25 | !default.perspectivev3 26 | 27 | xcuserdata 28 | 29 | *.moved-aside 30 | 31 | *.pyc 32 | *sync/ 33 | Icon? 34 | .tags* 35 | 36 | /Flutter/Generated.xcconfig 37 | /Flutter/ephemeral/ 38 | /Flutter/flutter_export_environment.sh 39 | -------------------------------------------------------------------------------- /ios/Assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PostHog/posthog-flutter/2db826f580f4723fffc90c4ba7115369d7159533/ios/Assets/.gitkeep -------------------------------------------------------------------------------- /ios/Classes/PostHogFlutterVersion.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PostHogFlutterVersion.swift 3 | // posthog_flutter 4 | // 5 | // Created by Manoel Aranda Neto on 08.01.24. 6 | // 7 | 8 | import Foundation 9 | 10 | // This property is internal only 11 | let postHogFlutterVersion = "5.0.0" 12 | -------------------------------------------------------------------------------- /ios/Resources/PrivacyInfo.xcprivacy: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSPrivacyCollectedDataTypes 6 | 7 | 8 | NSPrivacyCollectedDataType 9 | NSPrivacyCollectedDataTypeProductInteraction 10 | NSPrivacyCollectedDataTypeLinked 11 | 12 | NSPrivacyCollectedDataTypeTracking 13 | 14 | NSPrivacyCollectedDataTypePurposes 15 | 16 | NSPrivacyCollectedDataTypePurposeAnalytics 17 | 18 | 19 | 20 | NSPrivacyCollectedDataType 21 | NSPrivacyCollectedDataTypeOtherUsageData 22 | NSPrivacyCollectedDataTypeLinked 23 | 24 | NSPrivacyCollectedDataTypeTracking 25 | 26 | NSPrivacyCollectedDataTypePurposes 27 | 28 | NSPrivacyCollectedDataTypePurposeAnalytics 29 | 30 | 31 | 32 | NSPrivacyAccessedAPITypes 33 | 34 | 35 | NSPrivacyAccessedAPIType 36 | NSPrivacyAccessedAPICategoryUserDefaults 37 | NSPrivacyAccessedAPITypeReasons 38 | 39 | CA92.1 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /ios/posthog_flutter.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. 3 | # Run `pod lib lint posthog_flutter.podspec` to validate before publishing. 4 | # 5 | Pod::Spec.new do |s| 6 | s.name = 'posthog_flutter' 7 | s.version = '0.0.1' 8 | s.summary = 'The hassle-free way to add posthog to your Flutter app.' 9 | s.description = <<-DESC 10 | Postog flutter plugin 11 | DESC 12 | s.homepage = 'https://posthog.com/' 13 | s.license = { :file => '../LICENSE' } 14 | s.author = { 'PostHog' => 'engineering@posthog.com' } 15 | s.source = { :path => '.' } 16 | s.social_media_url = 'https://twitter.com/PostHog' 17 | 18 | s.source_files = 'Classes/**/*' 19 | s.resource_bundles = { "PostHogFlutter" => "Resources/PrivacyInfo.xcprivacy" } 20 | 21 | s.ios.dependency 'Flutter' 22 | s.osx.dependency 'FlutterMacOS' 23 | 24 | # ~> Version 3.22.0 up to, but not including, 4.0.0 25 | s.dependency 'PostHog', '~> 3.22' 26 | 27 | s.ios.deployment_target = '13.0' 28 | # PH iOS SDK 3.0.0 requires >= 10.15 29 | s.osx.deployment_target = '10.15' 30 | 31 | # Flutter.framework does not contain a i386 slice. 32 | s.ios.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } 33 | s.osx.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } 34 | s.swift_version = '5.3' 35 | end 36 | -------------------------------------------------------------------------------- /ktlint-baseline.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /lib/posthog_flutter.dart: -------------------------------------------------------------------------------- 1 | library posthog_flutter; 2 | 3 | export 'src/posthog.dart'; 4 | export 'src/posthog_config.dart'; 5 | export 'src/posthog_observer.dart'; 6 | export 'src/posthog_widget.dart'; 7 | export 'src/replay/mask/posthog_mask_widget.dart'; 8 | -------------------------------------------------------------------------------- /lib/posthog_flutter_web.dart: -------------------------------------------------------------------------------- 1 | // In order to *not* need this ignore, consider extracting the "web" version 2 | // of your plugin as a separate package, instead of inlining it in the same 3 | // package as the core of your plugin. 4 | import 'package:flutter/services.dart'; 5 | import 'package:flutter_web_plugins/flutter_web_plugins.dart'; 6 | 7 | import 'src/posthog_flutter_platform_interface.dart'; 8 | import 'src/posthog_flutter_web_handler.dart'; 9 | 10 | /// A web implementation of the PosthogFlutterPlatform of the PosthogFlutter plugin. 11 | class PosthogFlutterWeb extends PosthogFlutterPlatformInterface { 12 | /// Constructs a PosthogFlutterWeb 13 | PosthogFlutterWeb(); 14 | 15 | static void registerWith(Registrar registrar) { 16 | final MethodChannel channel = MethodChannel( 17 | 'posthog_flutter', 18 | const StandardMethodCodec(), 19 | registrar, 20 | ); 21 | final PosthogFlutterWeb instance = PosthogFlutterWeb(); 22 | channel.setMethodCallHandler(instance.handleMethodCall); 23 | } 24 | 25 | Future handleMethodCall(MethodCall call) => 26 | handleWebMethodCall(call); 27 | } 28 | -------------------------------------------------------------------------------- /lib/src/posthog.dart: -------------------------------------------------------------------------------- 1 | import 'package:meta/meta.dart'; 2 | 3 | import 'posthog_config.dart'; 4 | import 'posthog_flutter_platform_interface.dart'; 5 | import 'posthog_observer.dart'; 6 | 7 | class Posthog { 8 | static PosthogFlutterPlatformInterface get _posthog => 9 | PosthogFlutterPlatformInterface.instance; 10 | 11 | static final _instance = Posthog._internal(); 12 | 13 | PostHogConfig? _config; 14 | 15 | factory Posthog() { 16 | return _instance; 17 | } 18 | 19 | String? _currentScreen; 20 | 21 | /// Android and iOS only 22 | /// Only used for the manual setup 23 | /// Requires disabling the automatic init on Android and iOS: 24 | /// com.posthog.posthog.AUTO_INIT: false 25 | Future setup(PostHogConfig config) { 26 | _config = config; // Store the config 27 | return _posthog.setup(config); 28 | } 29 | 30 | @internal 31 | PostHogConfig? get config => _config; 32 | 33 | /// Returns the current screen name (or route name) 34 | /// Only returns a value if [PosthogObserver] is used 35 | @internal 36 | String? get currentScreen => _currentScreen; 37 | 38 | Future identify({ 39 | required String userId, 40 | Map? userProperties, 41 | Map? userPropertiesSetOnce, 42 | }) => 43 | _posthog.identify( 44 | userId: userId, 45 | userProperties: userProperties, 46 | userPropertiesSetOnce: userPropertiesSetOnce); 47 | 48 | Future capture({ 49 | required String eventName, 50 | Map? properties, 51 | }) { 52 | final propertiesCopy = properties == null ? null : {...properties}; 53 | 54 | final currentScreen = _currentScreen; 55 | if (propertiesCopy != null && 56 | !propertiesCopy.containsKey('\$screen_name') && 57 | currentScreen != null) { 58 | propertiesCopy['\$screen_name'] = currentScreen; 59 | } 60 | return _posthog.capture( 61 | eventName: eventName, 62 | properties: propertiesCopy, 63 | ); 64 | } 65 | 66 | Future screen({ 67 | required String screenName, 68 | Map? properties, 69 | }) { 70 | _currentScreen = screenName; 71 | return _posthog.screen( 72 | screenName: screenName, 73 | properties: properties, 74 | ); 75 | } 76 | 77 | Future alias({ 78 | required String alias, 79 | }) => 80 | _posthog.alias( 81 | alias: alias, 82 | ); 83 | 84 | Future getDistinctId() => _posthog.getDistinctId(); 85 | 86 | Future reset() => _posthog.reset(); 87 | 88 | Future disable() => _posthog.disable(); 89 | 90 | Future enable() => _posthog.enable(); 91 | 92 | Future debug(bool enabled) => _posthog.debug(enabled); 93 | 94 | Future register(String key, Object value) => 95 | _posthog.register(key, value); 96 | 97 | Future unregister(String key) => _posthog.unregister(key); 98 | 99 | Future isFeatureEnabled(String key) => _posthog.isFeatureEnabled(key); 100 | 101 | Future reloadFeatureFlags() => _posthog.reloadFeatureFlags(); 102 | 103 | Future group({ 104 | required String groupType, 105 | required String groupKey, 106 | Map? groupProperties, 107 | }) => 108 | _posthog.group( 109 | groupType: groupType, 110 | groupKey: groupKey, 111 | groupProperties: groupProperties, 112 | ); 113 | 114 | Future getFeatureFlag(String key) => 115 | _posthog.getFeatureFlag(key: key); 116 | 117 | Future getFeatureFlagPayload(String key) => 118 | _posthog.getFeatureFlagPayload(key: key); 119 | 120 | Future flush() => _posthog.flush(); 121 | 122 | Future close() { 123 | _config = null; 124 | _currentScreen = null; 125 | return _posthog.close(); 126 | } 127 | 128 | Future getSessionId() => _posthog.getSessionId(); 129 | 130 | Posthog._internal(); 131 | } 132 | -------------------------------------------------------------------------------- /lib/src/posthog_config.dart: -------------------------------------------------------------------------------- 1 | enum PostHogPersonProfiles { never, always, identifiedOnly } 2 | 3 | enum PostHogDataMode { wifi, cellular, any } 4 | 5 | class PostHogConfig { 6 | final String apiKey; 7 | var host = 'https://us.i.posthog.com'; 8 | var flushAt = 20; 9 | var maxQueueSize = 1000; 10 | var maxBatchSize = 50; 11 | var flushInterval = const Duration(seconds: 30); 12 | var sendFeatureFlagEvents = true; 13 | var preloadFeatureFlags = true; 14 | var captureApplicationLifecycleEvents = false; 15 | var debug = false; 16 | var optOut = false; 17 | var personProfiles = PostHogPersonProfiles.identifiedOnly; 18 | 19 | /// Enable Recording of Session replay for Android and iOS. 20 | /// Requires Record user sessions to be enabled in the PostHog Project Settings. 21 | /// Defaults to false. 22 | var sessionReplay = false; 23 | 24 | /// Configurations for Session replay. 25 | /// [sessionReplay] has to be enabled for this to take effect. 26 | var sessionReplayConfig = PostHogSessionReplayConfig(); 27 | 28 | /// iOS only 29 | var dataMode = PostHogDataMode.any; 30 | 31 | // TODO: missing getAnonymousId, propertiesSanitizer, captureDeepLinks 32 | // onFeatureFlags, integrations 33 | 34 | PostHogConfig(this.apiKey); 35 | 36 | Map toMap() { 37 | return { 38 | 'apiKey': apiKey, 39 | 'host': host, 40 | 'flushAt': flushAt, 41 | 'maxQueueSize': maxQueueSize, 42 | 'maxBatchSize': maxBatchSize, 43 | 'flushInterval': flushInterval.inSeconds, 44 | 'sendFeatureFlagEvents': sendFeatureFlagEvents, 45 | 'preloadFeatureFlags': preloadFeatureFlags, 46 | 'captureApplicationLifecycleEvents': captureApplicationLifecycleEvents, 47 | 'debug': debug, 48 | 'optOut': optOut, 49 | 'personProfiles': personProfiles.name, 50 | 'sessionReplay': sessionReplay, 51 | 'dataMode': dataMode.name, 52 | 'sessionReplayConfig': sessionReplayConfig.toMap(), 53 | }; 54 | } 55 | } 56 | 57 | class PostHogSessionReplayConfig { 58 | /// Enable masking of all text and text input fields. 59 | /// Default: true. 60 | var maskAllTexts = true; 61 | 62 | /// Enable masking of all images. 63 | /// Default: true. 64 | var maskAllImages = true; 65 | 66 | /// The value assigned to this var will be forwarded to [throttleDelay] 67 | /// 68 | /// Debouncer delay used to reduce the number of snapshots captured and reduce performance impact. 69 | /// This is used for capturing the view as a screenshot. 70 | /// The lower the number, the more snapshots will be captured but higher the performance impact. 71 | /// Defaults to 1s. 72 | @Deprecated('Deprecated in favor of [throttleDelay] from v4.8.0.') 73 | set debouncerDelay(Duration debouncerDelay) { 74 | throttleDelay = debouncerDelay; 75 | } 76 | 77 | /// Throttling delay used to reduce the number of snapshots captured and reduce performance impact. 78 | /// This is used for capturing the view as a screenshot. 79 | /// The lower the number, the more snapshots will be captured but higher the performance impact. 80 | /// Defaults to 1s. 81 | var throttleDelay = const Duration(seconds: 1); 82 | 83 | Map toMap() { 84 | return { 85 | 'maskAllImages': maskAllImages, 86 | 'maskAllTexts': maskAllTexts, 87 | 'throttleDelayMs': throttleDelay.inMilliseconds, 88 | }; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /lib/src/posthog_flutter_io.dart: -------------------------------------------------------------------------------- 1 | import 'util/platform_io_stub.dart' 2 | if (dart.library.io) 'util/platform_io_real.dart'; 3 | 4 | import 'package:flutter/services.dart'; 5 | import 'package:posthog_flutter/src/util/logging.dart'; 6 | 7 | import 'posthog_config.dart'; 8 | import 'posthog_flutter_platform_interface.dart'; 9 | 10 | /// An implementation of [PosthogFlutterPlatformInterface] that uses method channels. 11 | class PosthogFlutterIO extends PosthogFlutterPlatformInterface { 12 | /// The method channel used to interact with the native platform. 13 | final _methodChannel = const MethodChannel('posthog_flutter'); 14 | 15 | @override 16 | Future setup(PostHogConfig config) async { 17 | if (!isSupportedPlatform()) { 18 | return; 19 | } 20 | 21 | try { 22 | await _methodChannel.invokeMethod('setup', config.toMap()); 23 | } on PlatformException catch (exception) { 24 | printIfDebug('Exeption on setup: $exception'); 25 | } 26 | } 27 | 28 | @override 29 | Future identify({ 30 | required String userId, 31 | Map? userProperties, 32 | Map? userPropertiesSetOnce, 33 | }) async { 34 | if (!isSupportedPlatform()) { 35 | return; 36 | } 37 | 38 | try { 39 | await _methodChannel.invokeMethod('identify', { 40 | 'userId': userId, 41 | if (userProperties != null) 'userProperties': userProperties, 42 | if (userPropertiesSetOnce != null) 43 | 'userPropertiesSetOnce': userPropertiesSetOnce, 44 | }); 45 | } on PlatformException catch (exception) { 46 | printIfDebug('Exeption on identify: $exception'); 47 | } 48 | } 49 | 50 | @override 51 | Future capture({ 52 | required String eventName, 53 | Map? properties, 54 | }) async { 55 | if (!isSupportedPlatform()) { 56 | return; 57 | } 58 | 59 | try { 60 | await _methodChannel.invokeMethod('capture', { 61 | 'eventName': eventName, 62 | if (properties != null) 'properties': properties, 63 | }); 64 | } on PlatformException catch (exception) { 65 | printIfDebug('Exeption on capture: $exception'); 66 | } 67 | } 68 | 69 | @override 70 | Future screen({ 71 | required String screenName, 72 | Map? properties, 73 | }) async { 74 | if (!isSupportedPlatform()) { 75 | return; 76 | } 77 | 78 | try { 79 | await _methodChannel.invokeMethod('screen', { 80 | 'screenName': screenName, 81 | if (properties != null) 'properties': properties, 82 | }); 83 | } on PlatformException catch (exception) { 84 | printIfDebug('Exeption on screen: $exception'); 85 | } 86 | } 87 | 88 | @override 89 | Future alias({ 90 | required String alias, 91 | }) async { 92 | if (!isSupportedPlatform()) { 93 | return; 94 | } 95 | 96 | try { 97 | await _methodChannel.invokeMethod('alias', { 98 | 'alias': alias, 99 | }); 100 | } on PlatformException catch (exception) { 101 | printIfDebug('Exeption on alias: $exception'); 102 | } 103 | } 104 | 105 | @override 106 | Future getDistinctId() async { 107 | if (!isSupportedPlatform()) { 108 | return ""; 109 | } 110 | 111 | try { 112 | return await _methodChannel.invokeMethod('distinctId'); 113 | } on PlatformException catch (exception) { 114 | printIfDebug('Exeption on getDistinctId: $exception'); 115 | return ""; 116 | } 117 | } 118 | 119 | @override 120 | Future reset() async { 121 | if (!isSupportedPlatform()) { 122 | return; 123 | } 124 | 125 | try { 126 | await _methodChannel.invokeMethod('reset'); 127 | } on PlatformException catch (exception) { 128 | printIfDebug('Exeption on reset: $exception'); 129 | } 130 | } 131 | 132 | @override 133 | Future disable() async { 134 | if (!isSupportedPlatform()) { 135 | return; 136 | } 137 | 138 | try { 139 | await _methodChannel.invokeMethod('disable'); 140 | } on PlatformException catch (exception) { 141 | printIfDebug('Exeption on disable: $exception'); 142 | } 143 | } 144 | 145 | @override 146 | Future enable() async { 147 | if (!isSupportedPlatform()) { 148 | return; 149 | } 150 | 151 | try { 152 | await _methodChannel.invokeMethod('enable'); 153 | } on PlatformException catch (exception) { 154 | printIfDebug('Exeption on enable: $exception'); 155 | } 156 | } 157 | 158 | @override 159 | Future debug(bool enabled) async { 160 | if (!isSupportedPlatform()) { 161 | return; 162 | } 163 | 164 | try { 165 | await _methodChannel.invokeMethod('debug', { 166 | 'debug': enabled, 167 | }); 168 | } on PlatformException catch (exception) { 169 | printIfDebug('Exeption on debug: $exception'); 170 | } 171 | } 172 | 173 | @override 174 | Future isFeatureEnabled(String key) async { 175 | if (!isSupportedPlatform()) { 176 | return false; 177 | } 178 | 179 | try { 180 | return await _methodChannel.invokeMethod('isFeatureEnabled', { 181 | 'key': key, 182 | }); 183 | } on PlatformException catch (exception) { 184 | printIfDebug('Exeption on isFeatureEnabled: $exception'); 185 | return false; 186 | } 187 | } 188 | 189 | @override 190 | Future reloadFeatureFlags() async { 191 | if (!isSupportedPlatform()) { 192 | return; 193 | } 194 | 195 | try { 196 | await _methodChannel.invokeMethod('reloadFeatureFlags'); 197 | } on PlatformException catch (exception) { 198 | printIfDebug('Exeption on reloadFeatureFlags: $exception'); 199 | } 200 | } 201 | 202 | @override 203 | Future group({ 204 | required String groupType, 205 | required String groupKey, 206 | Map? groupProperties, 207 | }) async { 208 | if (!isSupportedPlatform()) { 209 | return; 210 | } 211 | 212 | try { 213 | await _methodChannel.invokeMethod('group', { 214 | 'groupType': groupType, 215 | 'groupKey': groupKey, 216 | if (groupProperties != null) 'groupProperties': groupProperties, 217 | }); 218 | } on PlatformException catch (exception) { 219 | printIfDebug('Exeption on group: $exception'); 220 | } 221 | } 222 | 223 | @override 224 | Future getFeatureFlag({ 225 | required String key, 226 | }) async { 227 | if (!isSupportedPlatform()) { 228 | return null; 229 | } 230 | 231 | try { 232 | return await _methodChannel.invokeMethod('getFeatureFlag', { 233 | 'key': key, 234 | }); 235 | } on PlatformException catch (exception) { 236 | printIfDebug('Exeption on getFeatureFlag: $exception'); 237 | return null; 238 | } 239 | } 240 | 241 | @override 242 | Future getFeatureFlagPayload({ 243 | required String key, 244 | }) async { 245 | if (!isSupportedPlatform()) { 246 | return null; 247 | } 248 | 249 | try { 250 | return await _methodChannel.invokeMethod('getFeatureFlagPayload', { 251 | 'key': key, 252 | }); 253 | } on PlatformException catch (exception) { 254 | printIfDebug('Exeption on getFeatureFlagPayload: $exception'); 255 | return null; 256 | } 257 | } 258 | 259 | @override 260 | Future register(String key, Object value) async { 261 | if (!isSupportedPlatform()) { 262 | return; 263 | } 264 | 265 | try { 266 | return await _methodChannel 267 | .invokeMethod('register', {'key': key, 'value': value}); 268 | } on PlatformException catch (exception) { 269 | printIfDebug('Exeption on register: $exception'); 270 | } 271 | } 272 | 273 | @override 274 | Future unregister(String key) async { 275 | if (!isSupportedPlatform()) { 276 | return; 277 | } 278 | 279 | try { 280 | return await _methodChannel.invokeMethod('unregister', {'key': key}); 281 | } on PlatformException catch (exception) { 282 | printIfDebug('Exeption on unregister: $exception'); 283 | } 284 | } 285 | 286 | @override 287 | Future flush() async { 288 | if (!isSupportedPlatform()) { 289 | return; 290 | } 291 | 292 | try { 293 | return await _methodChannel.invokeMethod('flush'); 294 | } on PlatformException catch (exception) { 295 | printIfDebug('Exeption on flush: $exception'); 296 | } 297 | } 298 | 299 | @override 300 | Future close() async { 301 | if (!isSupportedPlatform()) { 302 | return; 303 | } 304 | 305 | try { 306 | return await _methodChannel.invokeMethod('close'); 307 | } on PlatformException catch (exception) { 308 | printIfDebug('Exeption on close: $exception'); 309 | } 310 | } 311 | 312 | @override 313 | Future getSessionId() async { 314 | if (!isSupportedPlatform()) { 315 | return null; 316 | } 317 | 318 | try { 319 | final sessionId = await _methodChannel.invokeMethod('getSessionId'); 320 | return sessionId; 321 | } on PlatformException catch (exception) { 322 | printIfDebug('Exception on getSessionId: $exception'); 323 | return null; 324 | } 325 | } 326 | } 327 | -------------------------------------------------------------------------------- /lib/src/posthog_flutter_platform_interface.dart: -------------------------------------------------------------------------------- 1 | import 'package:plugin_platform_interface/plugin_platform_interface.dart'; 2 | 3 | import 'posthog_config.dart'; 4 | import 'posthog_flutter_io.dart'; 5 | 6 | abstract class PosthogFlutterPlatformInterface extends PlatformInterface { 7 | /// Constructs a PosthogFlutterPlatform. 8 | PosthogFlutterPlatformInterface() : super(token: _token); 9 | 10 | static final Object _token = Object(); 11 | 12 | static PosthogFlutterPlatformInterface _instance = PosthogFlutterIO(); 13 | 14 | /// The default instance of [PosthogFlutterPlatformInterface] to use. 15 | /// 16 | /// Defaults to [PosthogFlutterIO]. 17 | static PosthogFlutterPlatformInterface get instance => _instance; 18 | 19 | /// Platform-specific implementations should set this with their own 20 | /// platform-specific class that extends [PosthogFlutterPlatformInterface] when 21 | /// they register themselves. 22 | static set instance(PosthogFlutterPlatformInterface instance) { 23 | PlatformInterface.verifyToken(instance, _token); 24 | _instance = instance; 25 | } 26 | 27 | Future setup(PostHogConfig config) { 28 | throw UnimplementedError('setup() has not been implemented.'); 29 | } 30 | 31 | Future identify( 32 | {required String userId, 33 | Map? userProperties, 34 | Map? userPropertiesSetOnce}) { 35 | throw UnimplementedError('identify() has not been implemented.'); 36 | } 37 | 38 | Future capture({ 39 | required String eventName, 40 | Map? properties, 41 | }) { 42 | throw UnimplementedError('capture() has not been implemented.'); 43 | } 44 | 45 | Future screen({ 46 | required String screenName, 47 | Map? properties, 48 | }) { 49 | throw UnimplementedError('screen() has not been implemented.'); 50 | } 51 | 52 | Future alias({ 53 | required String alias, 54 | }) { 55 | throw UnimplementedError('alias() has not been implemented.'); 56 | } 57 | 58 | Future getDistinctId() { 59 | throw UnimplementedError('getDistinctId() has not been implemented.'); 60 | } 61 | 62 | Future reset() { 63 | throw UnimplementedError('reset() has not been implemented.'); 64 | } 65 | 66 | Future disable() { 67 | throw UnimplementedError('disable() has not been implemented.'); 68 | } 69 | 70 | Future enable() { 71 | throw UnimplementedError('enable() has not been implemented.'); 72 | } 73 | 74 | Future debug(bool enabled) { 75 | throw UnimplementedError('debug() has not been implemented.'); 76 | } 77 | 78 | Future register(String key, Object value) { 79 | throw UnimplementedError('register() has not been implemented.'); 80 | } 81 | 82 | Future unregister(String key) { 83 | throw UnimplementedError('unregister() has not been implemented.'); 84 | } 85 | 86 | Future isFeatureEnabled(String key) { 87 | throw UnimplementedError('isFeatureEnabled() has not been implemented.'); 88 | } 89 | 90 | Future reloadFeatureFlags() { 91 | throw UnimplementedError('reloadFeatureFlags() has not been implemented.'); 92 | } 93 | 94 | Future group({ 95 | required String groupType, 96 | required String groupKey, 97 | Map? groupProperties, 98 | }) { 99 | throw UnimplementedError('group() has not been implemented.'); 100 | } 101 | 102 | Future getFeatureFlag({ 103 | required String key, 104 | }) { 105 | throw UnimplementedError('getFeatureFlag() has not been implemented.'); 106 | } 107 | 108 | Future getFeatureFlagPayload({ 109 | required String key, 110 | }) { 111 | throw UnimplementedError( 112 | 'getFeatureFlagPayload() has not been implemented.'); 113 | } 114 | 115 | Future flush() { 116 | throw UnimplementedError('flush() has not been implemented.'); 117 | } 118 | 119 | Future close() { 120 | throw UnimplementedError('close() has not been implemented.'); 121 | } 122 | 123 | Future getSessionId() async { 124 | throw UnimplementedError('getSessionId() not implemented'); 125 | } 126 | 127 | // TODO: missing capture with more parameters 128 | } 129 | -------------------------------------------------------------------------------- /lib/src/posthog_flutter_web_handler.dart: -------------------------------------------------------------------------------- 1 | import 'dart:js_interop'; 2 | 3 | import 'package:flutter/services.dart'; 4 | 5 | // Definition of the JS interface for PostHog 6 | @JS() 7 | @staticInterop 8 | class PostHog {} 9 | 10 | extension PostHogExtension on PostHog { 11 | external JSAny? identify( 12 | JSAny userId, JSAny properties, JSAny propertiesSetOnce); 13 | external JSAny? capture(JSAny eventName, JSAny properties); 14 | external JSAny? alias(JSAny alias); 15 | external JSAny? get_distinct_id(); 16 | external void reset(); 17 | external void debug(JSAny debug); 18 | external JSAny? isFeatureEnabled(JSAny key); 19 | external void group(JSAny type, JSAny key, JSAny properties); 20 | external void reloadFeatureFlags(); 21 | external void opt_in_capturing(); 22 | external void opt_out_capturing(); 23 | external JSAny? getFeatureFlag(JSAny key); 24 | external JSAny? getFeatureFlagPayload(JSAny key); 25 | external void register(JSAny properties); 26 | external void unregister(JSAny key); 27 | external JSAny? get_session_id(); 28 | } 29 | 30 | // Accessing PostHog from the window object 31 | @JS('window.posthog') 32 | external PostHog? get posthog; 33 | 34 | // Conversion functions 35 | JSAny stringToJSAny(String value) { 36 | return value.toJS; 37 | } 38 | 39 | JSAny boolToJSAny(bool value) { 40 | return value.toJS; 41 | } 42 | 43 | JSAny mapToJSAny(Map map) { 44 | return map.jsify() ?? JSObject(); 45 | } 46 | 47 | // Function for safely converting maps 48 | Map safeMapConversion(dynamic mapData) { 49 | if (mapData == null) { 50 | return {}; 51 | } 52 | 53 | if (mapData is Map) { 54 | return Map.from( 55 | mapData.map((key, value) => MapEntry(key.toString(), value))); 56 | } 57 | 58 | return {}; 59 | } 60 | 61 | Future handleWebMethodCall(MethodCall call) async { 62 | final args = call.arguments; 63 | 64 | switch (call.method) { 65 | case 'setup': 66 | // not supported on Web 67 | break; 68 | case 'identify': 69 | final userId = args['userId'] as String; 70 | final userProperties = safeMapConversion(args['userProperties']); 71 | final userPropertiesSetOnce = 72 | safeMapConversion(args['userPropertiesSetOnce']); 73 | 74 | posthog?.identify( 75 | stringToJSAny(userId), 76 | mapToJSAny(userProperties), 77 | mapToJSAny(userPropertiesSetOnce), 78 | ); 79 | break; 80 | case 'capture': 81 | final eventName = args['eventName'] as String; 82 | final properties = safeMapConversion(args['properties']); 83 | 84 | posthog?.capture( 85 | stringToJSAny(eventName), 86 | mapToJSAny(properties), 87 | ); 88 | break; 89 | case 'screen': 90 | final screenName = args['screenName'] as String; 91 | final properties = safeMapConversion(args['properties']); 92 | properties['\$screen_name'] = screenName; 93 | 94 | posthog?.capture( 95 | stringToJSAny('\$screen'), 96 | mapToJSAny(properties), 97 | ); 98 | break; 99 | case 'alias': 100 | final alias = args['alias'] as String; 101 | 102 | posthog?.alias( 103 | stringToJSAny(alias), 104 | ); 105 | break; 106 | case 'distinctId': 107 | final distinctId = posthog?.get_distinct_id(); 108 | return distinctId?.dartify() as String?; 109 | case 'reset': 110 | posthog?.reset(); 111 | break; 112 | case 'debug': 113 | final enabled = args['debug'] as bool; 114 | posthog?.debug(boolToJSAny(enabled)); 115 | break; 116 | case 'isFeatureEnabled': 117 | final key = args['key'] as String; 118 | final isFeatureEnabled = posthog 119 | ?.isFeatureEnabled( 120 | stringToJSAny(key), 121 | ) 122 | ?.dartify() as bool? ?? 123 | false; 124 | return isFeatureEnabled; 125 | case 'group': 126 | final groupType = args['groupType'] as String; 127 | final groupKey = args['groupKey'] as String; 128 | final groupProperties = safeMapConversion(args['groupProperties']); 129 | 130 | posthog?.group( 131 | stringToJSAny(groupType), 132 | stringToJSAny(groupKey), 133 | mapToJSAny(groupProperties), 134 | ); 135 | break; 136 | case 'reloadFeatureFlags': 137 | posthog?.reloadFeatureFlags(); 138 | break; 139 | case 'enable': 140 | posthog?.opt_in_capturing(); 141 | break; 142 | case 'disable': 143 | posthog?.opt_out_capturing(); 144 | break; 145 | case 'getFeatureFlag': 146 | final key = args['key'] as String; 147 | 148 | final featureFlag = posthog?.getFeatureFlag( 149 | stringToJSAny(key), 150 | ); 151 | return featureFlag?.dartify(); 152 | case 'getFeatureFlagPayload': 153 | final key = args['key'] as String; 154 | 155 | final featureFlag = posthog?.getFeatureFlagPayload( 156 | stringToJSAny(key), 157 | ); 158 | return featureFlag?.dartify(); 159 | case 'register': 160 | final key = args['key'] as String; 161 | final value = args['value']; 162 | final properties = {key: value}; 163 | 164 | posthog?.register( 165 | mapToJSAny(properties), 166 | ); 167 | break; 168 | case 'unregister': 169 | final key = args['key'] as String; 170 | 171 | posthog?.unregister( 172 | stringToJSAny(key), 173 | ); 174 | break; 175 | case 'getSessionId': 176 | final sessionId = posthog?.get_session_id()?.dartify() as String?; 177 | if (sessionId?.isEmpty == true) return null; 178 | return sessionId; 179 | case 'flush': 180 | // not supported on Web 181 | // analytics.callMethod('flush'); 182 | break; 183 | case 'close': 184 | // not supported on Web 185 | // analytics.callMethod('close'); 186 | break; 187 | case 'sendMetaEvent': 188 | // not supported on Web 189 | // Flutter Web uses the JS SDK for Session replay 190 | break; 191 | case 'sendFullSnapshot': 192 | // not supported on Web 193 | // Flutter Web uses the JS SDK for Session replay 194 | break; 195 | case 'isSessionReplayActive': 196 | // not supported on Web 197 | // Flutter Web uses the JS SDK for Session replay 198 | return false; 199 | default: 200 | throw PlatformException( 201 | code: 'Unimplemented', 202 | details: 203 | "The posthog plugin for web doesn't implement the method '${call.method}'", 204 | ); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /lib/src/posthog_observer.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | 3 | import 'posthog.dart'; 4 | 5 | typedef ScreenNameExtractor = String? Function(RouteSettings settings); 6 | 7 | /// [PostHogRouteFilter] allows to filter out routes that should not be tracked. 8 | /// 9 | /// By default, only [PageRoute]s are tracked. 10 | typedef PostHogRouteFilter = bool Function(Route? route); 11 | 12 | String? defaultNameExtractor(RouteSettings settings) => settings.name; 13 | 14 | bool defaultPostHogRouteFilter(Route? route) => route is PageRoute; 15 | 16 | class PosthogObserver extends RouteObserver> { 17 | PosthogObserver( 18 | {ScreenNameExtractor nameExtractor = defaultNameExtractor, 19 | PostHogRouteFilter routeFilter = defaultPostHogRouteFilter}) 20 | : _nameExtractor = nameExtractor, 21 | _routeFilter = routeFilter; 22 | 23 | final ScreenNameExtractor _nameExtractor; 24 | 25 | final PostHogRouteFilter _routeFilter; 26 | 27 | bool _isTrackeableRoute(String? name) { 28 | return name != null && name.trim().isNotEmpty; 29 | } 30 | 31 | void _sendScreenView(Route? route) { 32 | if (route == null) { 33 | return; 34 | } 35 | 36 | var screenName = _nameExtractor(route.settings); 37 | if (_isTrackeableRoute(screenName)) { 38 | // if the screen name is the root route, we send it as root ("/") instead of only "/" 39 | if (screenName == '/') { 40 | screenName = 'root (\'/\')'; 41 | } 42 | 43 | if (screenName == null) { 44 | return; 45 | } 46 | 47 | Posthog().screen(screenName: screenName); 48 | } 49 | } 50 | 51 | @override 52 | void didPush(Route route, Route? previousRoute) { 53 | super.didPush(route, previousRoute); 54 | 55 | if (!_routeFilter(route)) { 56 | return; 57 | } 58 | 59 | _sendScreenView(route); 60 | } 61 | 62 | @override 63 | void didReplace({Route? newRoute, Route? oldRoute}) { 64 | super.didReplace(newRoute: newRoute, oldRoute: oldRoute); 65 | 66 | if (!_routeFilter(newRoute)) { 67 | return; 68 | } 69 | 70 | _sendScreenView(newRoute); 71 | } 72 | 73 | @override 74 | void didPop(Route route, Route? previousRoute) { 75 | super.didPop(route, previousRoute); 76 | 77 | if (!_routeFilter(previousRoute)) { 78 | return; 79 | } 80 | 81 | _sendScreenView(previousRoute); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /lib/src/posthog_widget.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | 3 | import 'package:flutter/material.dart'; 4 | import 'package:posthog_flutter/posthog_flutter.dart'; 5 | import 'package:posthog_flutter/src/replay/mask/posthog_mask_controller.dart'; 6 | 7 | import 'replay/change_detector.dart'; 8 | import 'replay/native_communicator.dart'; 9 | import 'replay/screenshot/screenshot_capturer.dart'; 10 | 11 | class PostHogWidget extends StatefulWidget { 12 | final Widget child; 13 | 14 | const PostHogWidget({super.key, required this.child}); 15 | 16 | @override 17 | PostHogWidgetState createState() => PostHogWidgetState(); 18 | } 19 | 20 | class PostHogWidgetState extends State { 21 | ChangeDetector? _changeDetector; 22 | ScreenshotCapturer? _screenshotCapturer; 23 | NativeCommunicator? _nativeCommunicator; 24 | 25 | Timer? _throttleTimer; 26 | bool _isThrottling = false; 27 | Duration _throttleDuration = const Duration(milliseconds: 1000); 28 | 29 | @override 30 | void initState() { 31 | super.initState(); 32 | 33 | final config = Posthog().config; 34 | if (config == null || !config.sessionReplay) { 35 | return; 36 | } 37 | 38 | _throttleDuration = config.sessionReplayConfig.throttleDelay; 39 | 40 | _screenshotCapturer = ScreenshotCapturer(config); 41 | _nativeCommunicator = NativeCommunicator(); 42 | 43 | _changeDetector = ChangeDetector(_onChangeDetected); 44 | _changeDetector?.start(); 45 | } 46 | 47 | // This works as onRootViewsChangedListeners 48 | void _onChangeDetected() { 49 | if (_isThrottling) { 50 | // If throttling is active, ignore this call 51 | return; 52 | } 53 | 54 | // Start throttling 55 | _isThrottling = true; 56 | 57 | // Execute the snapshot generation 58 | _generateSnapshot(); 59 | 60 | _throttleTimer?.cancel(); 61 | // Reset throttling after the duration 62 | _throttleTimer = Timer(_throttleDuration, () { 63 | _isThrottling = false; 64 | }); 65 | } 66 | 67 | Future _generateSnapshot() async { 68 | // Ensure no asynchronous calls occur before this function, 69 | // as it relies on a consistent state. 70 | final imageInfo = await _screenshotCapturer?.captureScreenshot(); 71 | if (imageInfo == null) { 72 | return; 73 | } 74 | 75 | if (imageInfo.shouldSendMetaEvent) { 76 | await _nativeCommunicator?.sendMetaEvent( 77 | width: imageInfo.width, 78 | height: imageInfo.height, 79 | screen: Posthog().currentScreen); 80 | } 81 | 82 | await _nativeCommunicator?.sendFullSnapshot(imageInfo.imageBytes, 83 | id: imageInfo.id, x: imageInfo.x, y: imageInfo.y); 84 | } 85 | 86 | @override 87 | Widget build(BuildContext context) { 88 | return RepaintBoundary( 89 | key: PostHogMaskController.instance.containerKey, 90 | child: Column( 91 | children: [ 92 | Expanded(child: Container(child: widget.child)), 93 | ], 94 | ), 95 | ); 96 | } 97 | 98 | @override 99 | void dispose() { 100 | _throttleTimer?.cancel(); 101 | _throttleTimer = null; 102 | _changeDetector?.stop(); 103 | _changeDetector = null; 104 | _screenshotCapturer = null; 105 | _nativeCommunicator = null; 106 | 107 | super.dispose(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /lib/src/replay/change_detector.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:posthog_flutter/posthog_flutter.dart'; 3 | 4 | /// A class that detects changes in the UI and executes a callback when changes occur. 5 | /// 6 | /// The `ChangeDetector` continuously monitors the Flutter widget tree by scheduling 7 | /// a callback after each frame is rendered. This is useful when you need to perform 8 | /// an action whenever the UI updates. 9 | /// 10 | /// **Usage:** 11 | /// ```dart 12 | /// final changeDetector = ChangeDetector(() { 13 | /// // Code to execute when a UI change is detected. 14 | /// print('UI has updated.'); 15 | /// }); 16 | /// 17 | /// changeDetector.start(); 18 | /// ``` 19 | /// 20 | /// **Note:** Since the `onChange` callback is called after every frame, ensure that 21 | /// the operations performed are efficient to avoid impacting app performance. 22 | class ChangeDetector { 23 | final VoidCallback onChange; 24 | bool _isRunning = false; 25 | 26 | /// Creates a [ChangeDetector] with the given [onChange] callback. 27 | ChangeDetector(this.onChange); 28 | 29 | /// Starts the change detection process. 30 | /// 31 | /// This method schedules the [_onFrameRendered] callback to be called 32 | /// after each frame is rendered. 33 | void start() { 34 | if (!_isRunning) { 35 | _isRunning = true; 36 | WidgetsBinding.instance.addPostFrameCallback((_) { 37 | _onFrameRendered(); 38 | }); 39 | } 40 | } 41 | 42 | /// Stops the change detection process. 43 | /// 44 | /// This prevents the [onChange] callback from being called after each frame. 45 | void stop() { 46 | _isRunning = false; 47 | } 48 | 49 | /// Internal method called after each frame is rendered. 50 | /// 51 | /// Executes the [onChange] callback and schedules itself for the next frame 52 | /// if the change detector is still running. 53 | void _onFrameRendered() { 54 | final config = Posthog().config; 55 | if (config == null || !config.sessionReplay) { 56 | return; 57 | } 58 | 59 | if (!_isRunning) { 60 | return; 61 | } 62 | 63 | onChange(); 64 | WidgetsBinding.instance.addPostFrameCallback((_) { 65 | _onFrameRendered(); 66 | }); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/element_data.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:posthog_flutter/src/replay/mask/posthog_mask_widget.dart'; 3 | 4 | class ElementData { 5 | Rect rect; 6 | String type; 7 | List? children; 8 | Widget? widget; 9 | 10 | ElementData({ 11 | required this.rect, 12 | required this.type, 13 | this.children, 14 | this.widget, 15 | }); 16 | 17 | void addChildren(ElementData elementData) { 18 | children ??= []; 19 | children?.add(elementData); 20 | } 21 | 22 | List extractMaskWidgetRects() { 23 | final rects = []; 24 | _collectMaskWidgetRects(this, rects); 25 | return rects; 26 | } 27 | 28 | List extractRects({bool isRoot = true}) { 29 | List rects = []; 30 | 31 | if (children != null) { 32 | for (var child in children ?? []) { 33 | if (child.children == null) { 34 | rects.add(child); 35 | continue; 36 | } else if ((child.children?.length ?? 0) > 1) { 37 | for (var grandChild in child.children ?? []) { 38 | rects.add(grandChild); 39 | } 40 | } else { 41 | rects.add(child); 42 | } 43 | } 44 | } 45 | return rects; 46 | } 47 | 48 | void _collectMaskWidgetRects(ElementData element, List rectList) { 49 | if (!rectList.contains(element.rect)) { 50 | if (element.widget is PostHogMaskWidget) { 51 | rectList.add(element.rect); 52 | } 53 | } 54 | 55 | final children = element.children; 56 | if (children != null && children.isNotEmpty) { 57 | for (var child in children) { 58 | _collectMaskWidgetRects(child, rectList); 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/element_data_factory.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 3 | import 'package:posthog_flutter/src/replay/size_extension.dart'; 4 | 5 | class ElementDataFactory { 6 | /// Creates an ElementData object from an Element 7 | ElementData? createFromElement(Element element, String type) { 8 | final renderObject = element.renderObject; 9 | if (renderObject is RenderBox && 10 | renderObject.hasSize && 11 | renderObject.size.isValidSize) { 12 | final offset = renderObject.localToGlobal(Offset.zero); 13 | return ElementData( 14 | type: type, 15 | rect: Rect.fromLTWH( 16 | offset.dx, 17 | offset.dy, 18 | renderObject.size.width, 19 | renderObject.size.height, 20 | ), 21 | ); 22 | } 23 | return null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/element_object_parser.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:flutter/rendering.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 4 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser.dart'; 5 | import 'package:posthog_flutter/src/replay/mask/posthog_mask_controller.dart'; 6 | import 'package:posthog_flutter/src/replay/mask/posthog_mask_widget.dart'; 7 | 8 | class ElementObjectParser { 9 | final ElementParser _elementParser = ElementParser(); 10 | 11 | ElementData? relateRenderObject( 12 | ElementData activeElementData, 13 | Element element, 14 | ) { 15 | if (element.widget is PostHogMaskWidget) { 16 | final elementData = _elementParser.relate(element, activeElementData); 17 | 18 | if (elementData != null) { 19 | activeElementData.addChildren(elementData); 20 | return elementData; 21 | } 22 | } 23 | 24 | if (element.widget is Text) { 25 | final elementData = _elementParser.relate(element, activeElementData); 26 | 27 | if (elementData != null) { 28 | activeElementData.addChildren(elementData); 29 | return elementData; 30 | } 31 | } 32 | 33 | if (element.renderObject is RenderImage) { 34 | final dataType = element.renderObject.runtimeType.toString(); 35 | 36 | final parser = PostHogMaskController.instance.parsers[dataType]; 37 | if (parser != null) { 38 | final elementData = parser.relate(element, activeElementData); 39 | 40 | if (elementData != null) { 41 | activeElementData.addChildren(elementData); 42 | return elementData; 43 | } 44 | } 45 | } 46 | 47 | return null; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/element_parser.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:flutter/material.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 4 | import 'package:posthog_flutter/src/replay/size_extension.dart'; 5 | 6 | class ElementParser { 7 | ElementParser(); 8 | 9 | ElementData? relate( 10 | Element element, 11 | ElementData parentElementData, 12 | ) { 13 | final Rect? elementRect = buildElementRect(element, parentElementData.rect); 14 | if (elementRect == null) { 15 | return null; 16 | } 17 | 18 | final thisElementData = ElementData( 19 | type: element.widget.runtimeType.toString(), 20 | rect: elementRect, 21 | widget: element.widget); 22 | 23 | return thisElementData; 24 | } 25 | 26 | Rect? buildElementRect(Element element, Rect? parentRect) { 27 | final renderObject = element.renderObject; 28 | if (renderObject is RenderBox && 29 | renderObject.hasSize && 30 | renderObject.size.isValidSize) { 31 | final Offset offset = renderObject.localToGlobal(Offset.zero); 32 | return Rect.fromLTWH( 33 | offset.dx, 34 | offset.dy, 35 | renderObject.size.width, 36 | renderObject.size.height, 37 | ); 38 | } 39 | return null; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/element_parser_factory.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/rendering.dart'; 2 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/image_element/position_calculator.dart'; 4 | import 'package:posthog_flutter/src/replay/element_parsers/image_element/render_image_parser.dart'; 5 | import 'package:posthog_flutter/src/replay/element_parsers/image_element/scaler.dart'; 6 | 7 | abstract class ElementParserFactory { 8 | ElementParser createElementParser(Type type); 9 | } 10 | 11 | class DefaultElementParserFactory implements ElementParserFactory { 12 | @override 13 | ElementParser createElementParser(Type type) { 14 | if (type == RenderImage) { 15 | return RenderImageParser( 16 | scaler: ImageScaler(), 17 | positionCalculator: DefaultPositionCalculator(), 18 | ); 19 | } else if (type == RenderParagraph || type == RenderTransform) { 20 | return ElementParser(); 21 | } 22 | 23 | // Default fallback 24 | return ElementParser(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/element_parsers_const.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/rendering.dart'; 2 | import 'package:posthog_flutter/posthog_flutter.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser.dart'; 4 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser_factory.dart'; 5 | 6 | class ElementParsersConst { 7 | final ElementParserFactory _factory; 8 | final Map parsersMap = {}; 9 | 10 | ElementParsersConst(this._factory, PostHogSessionReplayConfig? config) { 11 | if (config?.maskAllImages ?? true) { 12 | registerElementParser(); 13 | } 14 | if (config?.maskAllTexts ?? true) { 15 | registerElementParser(); 16 | registerElementParser(); 17 | } 18 | } 19 | 20 | void registerElementParser() { 21 | parsersMap[getRuntimeType()] = _factory.createElementParser(T); 22 | } 23 | 24 | ElementParser? getParserForType(Type type) { 25 | return parsersMap[getRuntimeTypeForType(type)]; 26 | } 27 | 28 | static String getRuntimeType() => T.toString(); 29 | 30 | static String getRuntimeTypeForType(Type type) => type.toString(); 31 | } 32 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/image_element/position_calculator.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/rendering.dart'; 2 | 3 | abstract class PositionCalculator { 4 | double calculateLeftPosition(AlignmentGeometry alignment, Offset offset, 5 | double containerWidth, double renderBoxWidth); 6 | 7 | double calculateTopPosition(AlignmentGeometry alignment, Offset offset, 8 | double containerHeight, double renderBoxHeight); 9 | } 10 | 11 | class DefaultPositionCalculator implements PositionCalculator { 12 | @override 13 | double calculateLeftPosition(AlignmentGeometry alignment, Offset offset, 14 | double containerWidth, double renderBoxWidth) { 15 | if (alignment == Alignment.centerLeft || 16 | alignment == Alignment.bottomLeft || 17 | alignment == Alignment.topLeft) { 18 | return offset.dx; 19 | } else if (alignment == Alignment.bottomRight || 20 | alignment == Alignment.centerRight || 21 | alignment == Alignment.topRight) { 22 | return offset.dx + containerWidth - renderBoxWidth; 23 | } 24 | 25 | return offset.dx + (containerWidth - renderBoxWidth) / 2; 26 | } 27 | 28 | @override 29 | double calculateTopPosition(AlignmentGeometry alignment, Offset offset, 30 | double containerHeight, double renderBoxHeight) { 31 | if (alignment == Alignment.topLeft || 32 | alignment == Alignment.topCenter || 33 | alignment == Alignment.topRight) { 34 | return offset.dy; 35 | } else if (alignment == Alignment.bottomRight || 36 | alignment == Alignment.bottomLeft || 37 | alignment == Alignment.bottomCenter) { 38 | return offset.dy + containerHeight - renderBoxHeight; 39 | } 40 | 41 | return offset.dy + (containerHeight - renderBoxHeight) / 2; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/image_element/render_image_parser.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:flutter/rendering.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser.dart'; 4 | import 'package:posthog_flutter/src/replay/size_extension.dart'; 5 | import 'package:posthog_flutter/src/replay/image_extension.dart'; 6 | 7 | import 'position_calculator.dart'; 8 | import 'scaler.dart'; 9 | 10 | class RenderImageParser extends ElementParser { 11 | final Scaler _scaler; 12 | final PositionCalculator _positionCalculator; 13 | 14 | RenderImageParser({ 15 | required Scaler scaler, 16 | required PositionCalculator positionCalculator, 17 | }) : _scaler = scaler, 18 | _positionCalculator = positionCalculator; 19 | 20 | @override 21 | Rect? buildElementRect(Element element, Rect? parentRect) { 22 | final RenderImage renderImage = element.renderObject as RenderImage; 23 | final image = renderImage.image; 24 | if (!renderImage.hasSize || 25 | !renderImage.size.isValidSize || 26 | image == null || 27 | !image.isValidSize) { 28 | return null; 29 | } 30 | 31 | final offset = renderImage.localToGlobal(Offset.zero); 32 | final BoxFit fit = renderImage.fit ?? BoxFit.scaleDown; 33 | 34 | final Size size = _scaler.getScaledSize( 35 | image.width.toDouble(), 36 | image.height.toDouble(), 37 | renderImage.size, 38 | fit, 39 | ); 40 | 41 | if (!size.isValidSize) { 42 | return null; 43 | } 44 | 45 | final AlignmentGeometry alignment = renderImage.alignment; 46 | 47 | final double left = _positionCalculator.calculateLeftPosition( 48 | alignment, offset, renderImage.size.width, size.width); 49 | final double top = _positionCalculator.calculateTopPosition( 50 | alignment, offset, renderImage.size.height, size.height); 51 | 52 | return Rect.fromLTWH(left, top, size.width, size.height); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/image_element/scaler.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/rendering.dart'; 2 | 3 | abstract class Scaler { 4 | Size getScaledSize( 5 | double originalWidth, double originalHeight, Size targetSize, BoxFit fit); 6 | } 7 | 8 | class ImageScaler implements Scaler { 9 | double _getRatio(double originalWidth, double originalHeight) { 10 | return originalWidth <= 0 || originalHeight <= 0 11 | ? 1.0 12 | : originalWidth / originalHeight; 13 | } 14 | 15 | @override 16 | Size getScaledSize(double originalWidth, double originalHeight, 17 | Size targetSize, BoxFit fit) { 18 | final double aspectRatio = _getRatio(originalWidth, originalHeight); 19 | 20 | switch (fit) { 21 | case BoxFit.fill: 22 | return Size(targetSize.width, targetSize.height); 23 | case BoxFit.contain: 24 | if (targetSize.width / aspectRatio <= targetSize.height) { 25 | return Size(targetSize.width, targetSize.width / aspectRatio); 26 | } 27 | return Size(targetSize.height * aspectRatio, targetSize.height); 28 | case BoxFit.cover: 29 | if (targetSize.width / aspectRatio >= targetSize.height) { 30 | return Size(targetSize.width, targetSize.width / aspectRatio); 31 | } 32 | return Size(targetSize.height * aspectRatio, targetSize.height); 33 | case BoxFit.fitWidth: 34 | return Size(targetSize.width, targetSize.width / aspectRatio); 35 | case BoxFit.fitHeight: 36 | return Size(targetSize.height * aspectRatio, targetSize.height); 37 | case BoxFit.none: 38 | return Size(originalWidth, originalHeight); 39 | case BoxFit.scaleDown: 40 | if (originalWidth > targetSize.width || 41 | originalHeight > targetSize.height) { 42 | return getScaledSize( 43 | originalWidth, originalHeight, targetSize, BoxFit.contain); 44 | } 45 | return Size(originalWidth, originalHeight); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /lib/src/replay/element_parsers/root_element_provider.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | 3 | class RootElementProvider { 4 | Element? getRootElement(BuildContext context) { 5 | Element? rootElement; 6 | if (ModalRoute.of(context)?.isActive ?? false) { 7 | Navigator.of(context, rootNavigator: true) 8 | .context 9 | .visitChildElements((element) { 10 | rootElement = element; 11 | }); 12 | } else { 13 | context.visitChildElements((element) { 14 | rootElement = element; 15 | }); 16 | } 17 | return rootElement; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lib/src/replay/image_extension.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui' as ui; 2 | 3 | extension ImageExtension on ui.Image { 4 | bool get isValidSize => width > 0 && height > 0; 5 | } 6 | -------------------------------------------------------------------------------- /lib/src/replay/mask/image_mask_painter.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 3 | import 'package:posthog_flutter/src/replay/mask/posthog_mask_widget.dart'; 4 | 5 | class ImageMaskPainter { 6 | void drawMaskedImage( 7 | Canvas canvas, List items, double pixelRatio) { 8 | final paint = Paint()..style = PaintingStyle.fill; 9 | for (var elementData in items) { 10 | paint.color = Colors.black; 11 | if (elementData.widget is PostHogMaskWidget) { 12 | paint.color = Colors.black; 13 | } 14 | final scaled = Rect.fromLTRB( 15 | elementData.rect.left * pixelRatio, 16 | elementData.rect.top * pixelRatio, 17 | elementData.rect.right * pixelRatio, 18 | elementData.rect.bottom * pixelRatio); 19 | canvas.drawRect(scaled, paint); 20 | } 21 | } 22 | 23 | void drawMaskedImageWrapper( 24 | Canvas canvas, List items, double pixelRatio) { 25 | final paint = Paint()..style = PaintingStyle.fill; 26 | for (var rect in items) { 27 | paint.color = Colors.black; 28 | final scaled = Rect.fromLTRB( 29 | rect.left * pixelRatio, 30 | rect.top * pixelRatio, 31 | rect.right * pixelRatio, 32 | rect.bottom * pixelRatio); 33 | canvas.drawRect(scaled, paint); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /lib/src/replay/mask/posthog_mask_controller.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:posthog_flutter/posthog_flutter.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 4 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser.dart'; 5 | import 'package:posthog_flutter/src/replay/element_parsers/element_parser_factory.dart'; 6 | import 'package:posthog_flutter/src/replay/element_parsers/element_parsers_const.dart'; 7 | import 'package:posthog_flutter/src/replay/element_parsers/element_data_factory.dart'; 8 | import 'package:posthog_flutter/src/replay/element_parsers/element_object_parser.dart'; 9 | import 'package:posthog_flutter/src/replay/element_parsers/root_element_provider.dart'; 10 | import 'package:posthog_flutter/src/replay/mask/widget_elements_decipher.dart'; 11 | import 'package:posthog_flutter/src/util/logging.dart'; 12 | 13 | class PostHogMaskController { 14 | late final Map parsers; 15 | 16 | final GlobalKey containerKey = GlobalKey(); 17 | 18 | final WidgetElementsDecipher _widgetScraper; 19 | 20 | PostHogMaskController._privateConstructor(PostHogSessionReplayConfig? config) 21 | : _widgetScraper = WidgetElementsDecipher( 22 | elementDataFactory: ElementDataFactory(), 23 | elementObjectParser: ElementObjectParser(), 24 | rootElementProvider: RootElementProvider(), 25 | ) { 26 | parsers = 27 | ElementParsersConst(DefaultElementParserFactory(), config).parsersMap; 28 | } 29 | 30 | static final PostHogMaskController instance = 31 | PostHogMaskController._privateConstructor( 32 | Posthog().config?.sessionReplayConfig); 33 | 34 | /// Extracts a flattened list of [ElementData] objects representing the 35 | /// renderable elements in the widget tree. 36 | /// 37 | /// This method traverses the tree of [ElementData] objects and returns a 38 | /// list of elements that have no children or only one child. 39 | /// 40 | /// The method is designed to extract the elements that are directly 41 | /// renderable on the screen. 42 | /// 43 | /// **Returns:** 44 | /// - `List`: A list of [ElementData] objects representing the 45 | /// renderable elements. 46 | /// 47 | List? getCurrentWidgetsElements() { 48 | final context = containerKey.currentContext; 49 | 50 | if (context == null) { 51 | printIfDebug('Error: containerKey.currentContext is null.'); 52 | return null; 53 | } 54 | 55 | try { 56 | final widgetElementsTree = _widgetScraper.parseRenderTree(context); 57 | 58 | if (widgetElementsTree == null) { 59 | printIfDebug('Error: widgetElementsTree is null after parsing.'); 60 | return null; 61 | } 62 | 63 | return widgetElementsTree.extractRects(); 64 | } catch (e) { 65 | printIfDebug( 66 | 'Error during render tree parsing or rectangle extraction: $e'); 67 | return null; 68 | } 69 | } 70 | 71 | List? getPostHogWidgetWrapperElements() { 72 | final context = containerKey.currentContext; 73 | 74 | if (context == null) { 75 | printIfDebug('Error: containerKey.currentContext is null.'); 76 | return null; 77 | } 78 | 79 | try { 80 | final widgetElementsTree = _widgetScraper.parseRenderTree(context); 81 | 82 | if (widgetElementsTree == null) { 83 | printIfDebug('Error: widgetElementsTree is null after parsing.'); 84 | return null; 85 | } 86 | 87 | return widgetElementsTree.extractMaskWidgetRects(); 88 | } catch (e) { 89 | printIfDebug( 90 | 'Error during render tree parsing or rectangle extraction: $e'); 91 | return null; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /lib/src/replay/mask/posthog_mask_widget.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | class PostHogMaskWidget extends StatefulWidget { 4 | final Widget child; 5 | 6 | const PostHogMaskWidget({ 7 | super.key, 8 | required this.child, 9 | }); 10 | 11 | @override 12 | PostHogMaskWidgetState createState() => PostHogMaskWidgetState(); 13 | } 14 | 15 | class PostHogMaskWidgetState extends State { 16 | final GlobalKey _widgetKey = GlobalKey(); 17 | 18 | @override 19 | void initState() { 20 | super.initState(); 21 | } 22 | 23 | @override 24 | void dispose() { 25 | super.dispose(); 26 | } 27 | 28 | @override 29 | Widget build(BuildContext context) { 30 | return Container( 31 | key: _widgetKey, 32 | child: widget.child, 33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /lib/src/replay/mask/widget_elements_decipher.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 3 | import 'package:posthog_flutter/src/replay/element_parsers/element_data_factory.dart'; 4 | import 'package:posthog_flutter/src/replay/element_parsers/element_object_parser.dart'; 5 | import 'package:posthog_flutter/src/replay/element_parsers/root_element_provider.dart'; 6 | 7 | class WidgetElementsDecipher { 8 | late ElementData _rootElementData; 9 | 10 | final ElementDataFactory _elementDataFactory; 11 | final ElementObjectParser _elementObjectParser; 12 | final RootElementProvider _rootElementProvider; 13 | 14 | WidgetElementsDecipher({ 15 | required ElementDataFactory elementDataFactory, 16 | required ElementObjectParser elementObjectParser, 17 | required RootElementProvider rootElementProvider, 18 | }) : _elementDataFactory = elementDataFactory, 19 | _elementObjectParser = elementObjectParser, 20 | _rootElementProvider = rootElementProvider; 21 | 22 | ElementData? parseRenderTree( 23 | BuildContext context, 24 | ) { 25 | final rootElement = _rootElementProvider.getRootElement(context); 26 | if (rootElement == null) return null; 27 | 28 | final rootElementData = 29 | _elementDataFactory.createFromElement(rootElement, "Root"); 30 | if (rootElementData == null) return null; 31 | 32 | _rootElementData = rootElementData; 33 | 34 | _parseAllElements( 35 | _rootElementData, 36 | rootElement, 37 | ); 38 | 39 | return _rootElementData; 40 | } 41 | 42 | void _parseAllElements( 43 | ElementData activeElementData, 44 | Element element, 45 | ) { 46 | ElementData? newElementData = 47 | _elementObjectParser.relateRenderObject(activeElementData, element); 48 | 49 | element.debugVisitOnstageChildren((childElement) { 50 | _parseAllElements( 51 | newElementData ?? activeElementData, 52 | childElement, 53 | ); 54 | }); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /lib/src/replay/native_communicator.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/services.dart'; 2 | import 'package:posthog_flutter/src/util/logging.dart'; 3 | 4 | class NativeCommunicator { 5 | static const MethodChannel _channel = MethodChannel('posthog_flutter'); 6 | 7 | Future sendFullSnapshot(Uint8List imageBytes, 8 | {required int id, required int x, required int y}) async { 9 | try { 10 | await _channel.invokeMethod('sendFullSnapshot', { 11 | 'imageBytes': imageBytes, 12 | 'id': id, 13 | 'x': x, 14 | 'y': y, 15 | }); 16 | } catch (e) { 17 | printIfDebug('Error sending full snapshot to native: $e'); 18 | } 19 | } 20 | 21 | Future sendMetaEvent( 22 | {required int width, 23 | required int height, 24 | required String? screen}) async { 25 | try { 26 | await _channel.invokeMethod('sendMetaEvent', { 27 | 'width': width, 28 | 'height': height, 29 | 'screen': screen, 30 | }); 31 | } catch (e) { 32 | printIfDebug('Error sending full snapshot to native: $e'); 33 | } 34 | } 35 | 36 | Future isSessionReplayActive() async { 37 | try { 38 | return await _channel.invokeMethod('isSessionReplayActive'); 39 | } catch (e) { 40 | printIfDebug('Error sending full snapshot to native: $e'); 41 | return false; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /lib/src/replay/screenshot/screenshot_capturer.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'dart:math'; 3 | import 'dart:typed_data'; 4 | import 'dart:ui' as ui; 5 | import 'package:flutter/rendering.dart'; 6 | import 'package:flutter/scheduler.dart'; 7 | import 'package:posthog_flutter/posthog_flutter.dart'; 8 | import 'package:posthog_flutter/src/replay/element_parsers/element_data.dart'; 9 | import 'package:posthog_flutter/src/replay/image_extension.dart'; 10 | import 'package:posthog_flutter/src/replay/mask/image_mask_painter.dart'; 11 | import 'package:posthog_flutter/src/replay/mask/posthog_mask_controller.dart'; 12 | import 'package:posthog_flutter/src/replay/native_communicator.dart'; 13 | import 'package:posthog_flutter/src/replay/screenshot/snapshot_manager.dart'; 14 | import 'package:posthog_flutter/src/replay/size_extension.dart'; 15 | import 'package:posthog_flutter/src/replay/vendor/equality.dart'; 16 | import 'package:posthog_flutter/src/util/logging.dart'; 17 | 18 | class ImageInfo { 19 | final int id; 20 | final int x; 21 | final int y; 22 | final int width; 23 | final int height; 24 | final bool shouldSendMetaEvent; 25 | final Uint8List imageBytes; 26 | 27 | ImageInfo(this.id, this.x, this.y, this.width, this.height, 28 | this.shouldSendMetaEvent, this.imageBytes); 29 | } 30 | 31 | class ViewTreeSnapshotStatus { 32 | bool sentMetaEvent = false; 33 | Uint8List? imageBytes; 34 | ViewTreeSnapshotStatus(this.sentMetaEvent); 35 | } 36 | 37 | class ScreenshotCapturer { 38 | final PostHogConfig _config; 39 | final ImageMaskPainter _imageMaskPainter = ImageMaskPainter(); 40 | final _nativeCommunicator = NativeCommunicator(); 41 | final _snapshotManager = SnapshotManager(); 42 | 43 | ScreenshotCapturer(this._config); 44 | 45 | double _getPixelRatio({ 46 | int? width, 47 | int? height, 48 | required double srcWidth, 49 | required double srcHeight, 50 | }) { 51 | if (width == null || height == null || srcWidth <= 0 || srcHeight <= 0) { 52 | return 1.0; 53 | } 54 | return min(width / srcWidth, height / srcHeight); 55 | } 56 | 57 | Future _getImageBytes(ui.Image img) async { 58 | try { 59 | final ByteData? byteData = 60 | await img.toByteData(format: ui.ImageByteFormat.png); 61 | if (byteData == null || byteData.lengthInBytes == 0) { 62 | printIfDebug('Error: Failed to convert image to byte data.'); 63 | return null; 64 | } 65 | return byteData.buffer.asUint8List(); 66 | } catch (e) { 67 | printIfDebug('Error converting image to byte data: $e'); 68 | return null; 69 | } 70 | } 71 | 72 | Future captureScreenshot() { 73 | final context = PostHogMaskController.instance.containerKey.currentContext; 74 | if (context == null) { 75 | return Future.value(null); 76 | } 77 | 78 | final renderObject = context.findRenderObject() as RenderRepaintBoundary?; 79 | if (renderObject == null || 80 | !renderObject.hasSize || 81 | !renderObject.size.isValidSize) { 82 | return Future.value(null); 83 | } 84 | 85 | final statusView = _snapshotManager.getStatus(renderObject); 86 | 87 | final shouldSendMetaEvent = !statusView.sentMetaEvent; 88 | 89 | // Get the global position of the widget 90 | final globalPosition = renderObject.localToGlobal(Offset.zero); 91 | 92 | final viewId = identityHashCode(renderObject); 93 | 94 | final Completer completer = Completer(); 95 | 96 | try { 97 | final srcWidth = renderObject.size.width; 98 | final srcHeight = renderObject.size.height; 99 | final pixelRatio = 100 | _getPixelRatio(srcWidth: srcWidth, srcHeight: srcHeight); 101 | 102 | final syncImage = renderObject.toImage(pixelRatio: pixelRatio); 103 | 104 | final replayConfig = _config.sessionReplayConfig; 105 | 106 | final postHogWidgetWrapperElements = 107 | PostHogMaskController.instance.getPostHogWidgetWrapperElements(); 108 | 109 | // call getCurrentScreenRects if really necessary 110 | List? elementsDataWidgets; 111 | if (replayConfig.maskAllTexts || replayConfig.maskAllImages) { 112 | elementsDataWidgets = 113 | PostHogMaskController.instance.getCurrentWidgetsElements(); 114 | } 115 | 116 | /// we firstly get current image (syncImage) and masks 117 | /// (postHogWidgetWrapperElements, elementsDataWidgets) synchronously and 118 | /// then executed the main process asynchronous 119 | Future(() async { 120 | final isSessionReplayActive = 121 | await _nativeCommunicator.isSessionReplayActive(); 122 | 123 | // wait the UI to settle 124 | await SchedulerBinding.instance.endOfFrame; 125 | final image = await syncImage; 126 | if (!isSessionReplayActive || !image.isValidSize) { 127 | _snapshotManager.clear(); 128 | image.dispose(); 129 | completer.complete(null); 130 | return; 131 | } 132 | 133 | final recorder = ui.PictureRecorder(); 134 | final canvas = Canvas(recorder); 135 | 136 | // using png because its compressed, the native SDKs will decompress it 137 | // and transform to webp or jpeg if needed 138 | // https://github.com/brendan-duncan/image does not have webp encoding 139 | Uint8List? pngBytes = await _getImageBytes(image); 140 | if (pngBytes == null || pngBytes.isEmpty) { 141 | printIfDebug( 142 | 'Error: Failed to convert image byte data to Uint8List.'); 143 | recorder.endRecording().dispose(); 144 | image.dispose(); 145 | completer.complete(null); 146 | return; 147 | } 148 | 149 | if (const PHListEquality().equals(pngBytes, statusView.imageBytes)) { 150 | printIfDebug( 151 | 'Debug: Snapshot is the same as the last one, nothing changed, do nothing.'); 152 | recorder.endRecording().dispose(); 153 | image.dispose(); 154 | completer.complete(null); 155 | return; 156 | } 157 | 158 | statusView.imageBytes = pngBytes; 159 | 160 | try { 161 | canvas.drawImage(image, Offset.zero, Paint()); 162 | } finally { 163 | image.dispose(); 164 | } 165 | 166 | if (replayConfig.maskAllTexts || replayConfig.maskAllImages) { 167 | if (elementsDataWidgets != null && elementsDataWidgets.isNotEmpty) { 168 | _imageMaskPainter.drawMaskedImage( 169 | canvas, elementsDataWidgets, pixelRatio); 170 | } 171 | 172 | final picture = recorder.endRecording(); 173 | 174 | try { 175 | final finalImage = 176 | await picture.toImage(srcWidth.toInt(), srcHeight.toInt()); 177 | 178 | if (!finalImage.isValidSize) { 179 | finalImage.dispose(); 180 | picture.dispose(); 181 | completer.complete(null); 182 | return; 183 | } 184 | 185 | try { 186 | final maskedImagePngBytes = await _getImageBytes(finalImage); 187 | if (maskedImagePngBytes == null || maskedImagePngBytes.isEmpty) { 188 | finalImage.dispose(); 189 | picture.dispose(); 190 | completer.complete(null); 191 | return; 192 | } 193 | 194 | final imageInfo = ImageInfo( 195 | viewId, 196 | globalPosition.dx.toInt(), 197 | globalPosition.dy.toInt(), 198 | srcWidth.toInt(), 199 | srcHeight.toInt(), 200 | shouldSendMetaEvent, 201 | maskedImagePngBytes, 202 | ); 203 | _snapshotManager.updateStatus(renderObject, 204 | shouldSendMetaEvent: shouldSendMetaEvent); 205 | completer.complete(imageInfo); 206 | } finally { 207 | finalImage.dispose(); 208 | } 209 | } finally { 210 | picture.dispose(); 211 | } 212 | } else { 213 | if (postHogWidgetWrapperElements != null && 214 | postHogWidgetWrapperElements.isNotEmpty) { 215 | _imageMaskPainter.drawMaskedImageWrapper( 216 | canvas, postHogWidgetWrapperElements, pixelRatio); 217 | } 218 | 219 | final picture = recorder.endRecording(); 220 | 221 | try { 222 | final finalImage = 223 | await picture.toImage(srcWidth.toInt(), srcHeight.toInt()); 224 | 225 | if (!finalImage.isValidSize) { 226 | finalImage.dispose(); 227 | picture.dispose(); 228 | completer.complete(null); 229 | return; 230 | } 231 | 232 | try { 233 | final pngBytes = await _getImageBytes(finalImage); 234 | if (pngBytes == null || pngBytes.isEmpty) { 235 | finalImage.dispose(); 236 | picture.dispose(); 237 | completer.complete(null); 238 | return; 239 | } 240 | 241 | final imageInfo = ImageInfo( 242 | viewId, 243 | globalPosition.dx.toInt(), 244 | globalPosition.dy.toInt(), 245 | srcWidth.toInt(), 246 | srcHeight.toInt(), 247 | shouldSendMetaEvent, 248 | pngBytes, 249 | ); 250 | _snapshotManager.updateStatus(renderObject, 251 | shouldSendMetaEvent: shouldSendMetaEvent); 252 | completer.complete(imageInfo); 253 | } finally { 254 | finalImage.dispose(); 255 | } 256 | } finally { 257 | picture.dispose(); 258 | } 259 | } 260 | }).catchError((error) { 261 | printIfDebug('Error capturing image: $error'); 262 | if (!completer.isCompleted) { 263 | completer.complete(null); 264 | } 265 | }); 266 | 267 | return completer.future; 268 | } catch (e) { 269 | printIfDebug('Error initializing capture: $e'); 270 | return Future.value(null); 271 | } 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /lib/src/replay/screenshot/snapshot_manager.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:posthog_flutter/src/replay/screenshot/screenshot_capturer.dart'; 3 | 4 | class SnapshotManager { 5 | // Expando is the equivalent of weakref 6 | Expando _snapshotStatuses = Expando(); 7 | 8 | ViewTreeSnapshotStatus getStatus(RenderObject renderObject) { 9 | return _snapshotStatuses[renderObject] ??= ViewTreeSnapshotStatus(false); 10 | } 11 | 12 | void clear() { 13 | _snapshotStatuses = Expando(); 14 | } 15 | 16 | void updateStatus(RenderObject renderObject, 17 | {required bool shouldSendMetaEvent}) { 18 | final status = getStatus(renderObject); 19 | if (shouldSendMetaEvent) { 20 | status.sentMetaEvent = true; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /lib/src/replay/size_extension.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | extension SizeExtension on Size { 4 | bool get isValidSize => width > 0 && height > 0; 5 | } 6 | -------------------------------------------------------------------------------- /lib/src/replay/vendor/equality.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // Adapted from https://github.com/dart-lang/core/blob/7f9f597e64fa52faebd3c0a2214f61a7b081174d/pkgs/collection/lib/src/equality.dart#L164 6 | 7 | // ignore_for_file: library_private_types_in_public_api 8 | 9 | const int _hashMask = 0x7fffffff; 10 | 11 | /// A generic equality relation on objects. 12 | abstract class _Equality { 13 | const factory _Equality() = _DefaultEquality; 14 | 15 | /// Compare two elements for being equal. 16 | /// 17 | /// This should be a proper equality relation. 18 | bool equals(E e1, E e2); 19 | 20 | /// Get a hashcode of an element. 21 | /// 22 | /// The hashcode should be compatible with [equals], so that if 23 | /// `equals(a, b)` then `hash(a) == hash(b)`. 24 | int hash(E e); 25 | 26 | /// Test whether an object is a valid argument to [equals] and [hash]. 27 | /// 28 | /// Some implementations may be restricted to only work on specific types 29 | /// of objects. 30 | bool isValidKey(Object? o); 31 | } 32 | 33 | /// Equality of objects that compares only the natural equality of the objects. 34 | /// 35 | /// This equality uses the objects' own [Object.==] and [Object.hashCode] for 36 | /// the equality. 37 | /// 38 | /// Note that [equals] and [hash] take `Object`s rather than `E`s. This allows 39 | /// `E` to be inferred as `Null` in const contexts where `E` wouldn't be a 40 | /// compile-time constant, while still allowing the class to be used at runtime. 41 | class _DefaultEquality implements _Equality { 42 | const _DefaultEquality(); 43 | @override 44 | bool equals(Object? e1, Object? e2) => e1 == e2; 45 | @override 46 | int hash(Object? e) => e.hashCode; 47 | @override 48 | bool isValidKey(Object? o) => true; 49 | } 50 | 51 | /// Equality on lists. 52 | /// 53 | /// Two lists are equal if they have the same length and their elements 54 | /// at each index are equal. 55 | /// 56 | /// This is effectively the same as [IterableEquality] except that it 57 | /// accesses elements by index instead of through iteration. 58 | /// 59 | /// The [equals] and [hash] methods accepts `null` values, 60 | /// even if the [isValidKey] returns `false` for `null`. 61 | /// The [hash] of `null` is `null.hashCode`. 62 | class PHListEquality implements _Equality> { 63 | final _Equality _elementEquality; 64 | const PHListEquality( 65 | [_Equality elementEquality = const _DefaultEquality()]) 66 | : _elementEquality = elementEquality; 67 | 68 | @override 69 | bool equals(List? list1, List? list2) { 70 | if (identical(list1, list2)) return true; 71 | if (list1 == null || list2 == null) return false; 72 | var length = list1.length; 73 | if (length != list2.length) return false; 74 | for (var i = 0; i < length; i++) { 75 | if (!_elementEquality.equals(list1[i], list2[i])) return false; 76 | } 77 | return true; 78 | } 79 | 80 | @override 81 | int hash(List? list) { 82 | if (list == null) return null.hashCode; 83 | // Jenkins's one-at-a-time hash function. 84 | // This code is almost identical to the one in IterableEquality, except 85 | // that it uses indexing instead of iterating to get the elements. 86 | var hash = 0; 87 | for (var i = 0; i < list.length; i++) { 88 | var c = _elementEquality.hash(list[i]); 89 | hash = (hash + c) & _hashMask; 90 | hash = (hash + (hash << 10)) & _hashMask; 91 | hash ^= hash >> 6; 92 | } 93 | hash = (hash + (hash << 3)) & _hashMask; 94 | hash ^= hash >> 11; 95 | hash = (hash + (hash << 15)) & _hashMask; 96 | return hash; 97 | } 98 | 99 | @override 100 | bool isValidKey(Object? o) => o is List; 101 | } 102 | -------------------------------------------------------------------------------- /lib/src/util/logging.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/foundation.dart'; 2 | 3 | void printIfDebug(String message) { 4 | if (kDebugMode) { 5 | print(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /lib/src/util/platform_io_real.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | bool isSupportedPlatform() { 4 | return !(Platform.isLinux || Platform.isWindows); 5 | } 6 | -------------------------------------------------------------------------------- /lib/src/util/platform_io_stub.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/foundation.dart'; 2 | 3 | bool isSupportedPlatform() { 4 | return kIsWeb; 5 | } 6 | -------------------------------------------------------------------------------- /macos/Classes/PostHogFlutterVersion.swift: -------------------------------------------------------------------------------- 1 | ../../ios/Classes/PostHogFlutterVersion.swift -------------------------------------------------------------------------------- /macos/Classes/PosthogFlutterPlugin.swift: -------------------------------------------------------------------------------- 1 | ../../ios/Classes/PosthogFlutterPlugin.swift -------------------------------------------------------------------------------- /macos/Resources/PrivacyInfo.xcprivacy: -------------------------------------------------------------------------------- 1 | ../../ios/Resources/PrivacyInfo.xcprivacy -------------------------------------------------------------------------------- /macos/posthog_flutter.podspec: -------------------------------------------------------------------------------- 1 | ../ios/posthog_flutter.podspec -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: posthog_flutter 2 | description: Flutter implementation of PostHog client for iOS, Android and Web 3 | version: 5.0.0 4 | homepage: https://www.posthog.com 5 | repository: https://github.com/posthog/posthog-flutter 6 | issue_tracker: https://github.com/posthog/posthog-flutter/issues 7 | documentation: https://github.com/posthog/posthog-flutter#readme 8 | 9 | environment: 10 | sdk: '>=3.3.0 <4.0.0' 11 | flutter: '>=3.19.0' 12 | 13 | dependencies: 14 | flutter: 15 | sdk: flutter 16 | flutter_web_plugins: 17 | sdk: flutter 18 | plugin_platform_interface: ^2.0.2 19 | # plugin_platform_interface depends on meta anyway 20 | meta: ^1.3.0 21 | 22 | dev_dependencies: 23 | flutter_lints: ^5.0.0 24 | flutter_test: 25 | sdk: flutter 26 | 27 | # For information on the generic Dart part of this file, see the 28 | # following page: https://dart.dev/tools/pub/pubspec 29 | 30 | # The following section is specific to Flutter packages. 31 | flutter: 32 | # This section identifies this Flutter project as a plugin project. 33 | # The 'pluginClass' specifies the class (in Java, Kotlin, Swift, Objective-C, etc.) 34 | # which should be registered in the plugin registry. This is required for 35 | # using method channels. 36 | # The Android 'package' specifies package in which the registered class is. 37 | # This is required for using method channels on Android. 38 | # The 'ffiPlugin' specifies that native code should be built and bundled. 39 | # This is required for using `dart:ffi`. 40 | # All these are used by the tooling to maintain consistency when 41 | # adding or updating assets for this project. 42 | plugin: 43 | platforms: 44 | android: 45 | package: com.posthog.flutter 46 | pluginClass: PosthogFlutterPlugin 47 | ios: 48 | pluginClass: PosthogFlutterPlugin 49 | macos: 50 | pluginClass: PosthogFlutterPlugin 51 | web: 52 | pluginClass: PosthogFlutterWeb 53 | fileName: posthog_flutter_web.dart 54 | 55 | # To add assets to your plugin package, add an assets section, like this: 56 | # assets: 57 | # - images/a_dot_burr.jpeg 58 | # - images/a_dot_ham.jpeg 59 | # 60 | # For details regarding assets in packages, see 61 | # https://flutter.dev/assets-and-images/#from-packages 62 | # 63 | # An image asset can refer to one or more resolution-specific "variants", see 64 | # https://flutter.dev/assets-and-images/#resolution-aware 65 | 66 | # To add custom fonts to your plugin package, add a fonts section here, 67 | # in this "flutter" section. Each entry in this list should have a 68 | # "family" key with the font family name, and a "fonts" key with a 69 | # list giving the asset and other descriptors for the font. For 70 | # example: 71 | # fonts: 72 | # - family: Schyler 73 | # fonts: 74 | # - asset: fonts/Schyler-Regular.ttf 75 | # - asset: fonts/Schyler-Italic.ttf 76 | # style: italic 77 | # - family: Trajan Pro 78 | # fonts: 79 | # - asset: fonts/TrajanPro.ttf 80 | # - asset: fonts/TrajanPro_Bold.ttf 81 | # weight: 700 82 | # 83 | # For details regarding fonts in packages, see 84 | # https://flutter.dev/custom-fonts/#from-packages 85 | -------------------------------------------------------------------------------- /scripts/bump-version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ./scripts/bump-version.sh 4 | # eg ./scripts/bump-version.sh "3.0.0-alpha.1" 5 | 6 | set -eux 7 | 8 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 9 | cd $SCRIPT_DIR/.. 10 | 11 | NEW_VERSION="$1" 12 | 13 | # Replace iOS `postHogFlutterVersion` with the given version 14 | perl -pi -e "s/postHogFlutterVersion = \".*\"/postHogFlutterVersion = \"$NEW_VERSION\"/" ios/Classes/PostHogFlutterVersion.swift 15 | 16 | # Replace Android `postHogVersion` with the given version 17 | perl -pi -e "s/postHogVersion = \".*\"/postHogVersion = \"$NEW_VERSION\"/" android/src/main/kotlin/com/posthog/flutter/PostHogVersion.kt 18 | 19 | # Replace Flutter `version` with the given version 20 | perl -pi -e "s/^version: .*/version: $NEW_VERSION/" pubspec.yaml 21 | -------------------------------------------------------------------------------- /scripts/commit-code.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | 4 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 5 | cd $SCRIPT_DIR/.. 6 | 7 | if [[ $(git status) == *"nothing to commit"* ]]; then 8 | echo "Nothing to commit." 9 | else 10 | echo "Going to push the changes." 11 | git fetch 12 | git commit -am "Update version" 13 | git push 14 | fi 15 | -------------------------------------------------------------------------------- /scripts/prepare-release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eux 4 | 5 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 6 | cd $SCRIPT_DIR/.. 7 | 8 | NEW_VERSION="$1" 9 | 10 | # bump version 11 | ./scripts/bump-version.sh $NEW_VERSION 12 | 13 | # commit changes 14 | ./scripts/commit-code.sh 15 | -------------------------------------------------------------------------------- /test/posthog_flutter_platform_interface_fake.dart: -------------------------------------------------------------------------------- 1 | import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart'; 2 | 3 | class PosthogFlutterPlatformFake extends PosthogFlutterPlatformInterface { 4 | String? screenName; 5 | 6 | @override 7 | Future screen({ 8 | required String screenName, 9 | Map? properties, 10 | }) async { 11 | this.screenName = screenName; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/posthog_observer_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:posthog_flutter/posthog_flutter.dart'; 4 | import 'package:posthog_flutter/src/posthog_flutter_io.dart'; 5 | import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart'; 6 | import 'package:posthog_flutter/src/posthog_observer.dart'; 7 | 8 | import 'posthog_flutter_platform_interface_fake.dart'; 9 | 10 | void main() { 11 | PageRoute route(RouteSettings? settings) => PageRouteBuilder( 12 | pageBuilder: (_, __, ___) => Container(), 13 | settings: settings, 14 | ); 15 | 16 | final fake = PosthogFlutterPlatformFake(); 17 | 18 | setUp(() { 19 | TestWidgetsFlutterBinding.ensureInitialized(); 20 | PosthogFlutterPlatformInterface.instance = fake; 21 | }); 22 | 23 | tearDown(() { 24 | fake.screenName = null; 25 | PosthogFlutterPlatformInterface.instance = PosthogFlutterIO(); 26 | }); 27 | 28 | PosthogObserver getSut( 29 | {ScreenNameExtractor nameExtractor = defaultNameExtractor, 30 | PostHogRouteFilter routeFilter = defaultPostHogRouteFilter}) { 31 | return PosthogObserver( 32 | nameExtractor: nameExtractor, routeFilter: routeFilter); 33 | } 34 | 35 | test('returns current route name', () { 36 | final currentRoute = route(const RouteSettings(name: 'Current Route')); 37 | 38 | final sut = getSut(); 39 | sut.didPush(currentRoute, null); 40 | 41 | expect(fake.screenName, 'Current Route'); 42 | }); 43 | 44 | test('returns overriden route name', () { 45 | final currentRoute = route(const RouteSettings(name: 'Current Route')); 46 | 47 | String? nameExtractor(RouteSettings settings) => 'overriden'; 48 | 49 | final sut = getSut(nameExtractor: nameExtractor); 50 | sut.didPush(currentRoute, null); 51 | 52 | expect(fake.screenName, 'overriden'); 53 | }); 54 | 55 | test('returns overriden root route name', () { 56 | final currentRoute = route(const RouteSettings(name: '/')); 57 | 58 | final sut = getSut(); 59 | sut.didPush(currentRoute, null); 60 | 61 | expect(fake.screenName, 'root (\'/\')'); 62 | }); 63 | 64 | test('does not capture not named routes', () { 65 | final currentRoute = route(const RouteSettings(name: null)); 66 | 67 | final sut = getSut(); 68 | sut.didPush(currentRoute, null); 69 | 70 | expect(fake.screenName, null); 71 | }); 72 | 73 | test('does not capture blank routes', () { 74 | final currentRoute = route(const RouteSettings(name: ' ')); 75 | 76 | final sut = getSut(); 77 | sut.didPush(currentRoute, null); 78 | 79 | expect(fake.screenName, null); 80 | }); 81 | 82 | test('does not capture filtered routes', () { 83 | // CustomOverlawRoute isn't a PageRoute 84 | final overlayRoute = CustomOverlawRoute( 85 | settings: const RouteSettings(name: 'Overlay Route'), 86 | ); 87 | 88 | final sut = getSut(); 89 | sut.didPush(overlayRoute, null); 90 | 91 | expect(fake.screenName, null); 92 | }); 93 | 94 | test('allows overriding the route filter', () { 95 | final overlayRoute = CustomOverlawRoute( 96 | settings: const RouteSettings(name: 'Overlay Route'), 97 | ); 98 | 99 | bool defaultPostHogRouteFilter(Route? route) => 100 | route is PageRoute || route is OverlayRoute; 101 | 102 | final sut = getSut(routeFilter: defaultPostHogRouteFilter); 103 | sut.didPush(overlayRoute, null); 104 | 105 | expect(fake.screenName, 'Overlay Route'); 106 | }); 107 | } 108 | 109 | class CustomOverlawRoute extends OverlayRoute { 110 | CustomOverlawRoute({super.settings}); 111 | 112 | @override 113 | Iterable createOverlayEntries() { 114 | return []; 115 | } 116 | } 117 | --------------------------------------------------------------------------------