Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor root screen recording and do not mutate properties when calling capture and more bug fixing #74

Merged
merged 12 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Next

- Record the root view as `root ('/')` instead of not recording at all [#74](https://github.com/PostHog/posthog-flutter/pull/74)
- Do not mutate the given properties when calling capture [#74](https://github.com/PostHog/posthog-flutter/pull/74)
- Thanks @lukepighetti for the [PR](https://github.com/PostHog/posthog-flutter/pull/66)!
- Fix `CAPTURE_APPLICATION_LIFECYCLE_EVENTS` typo for iOS [#74](https://github.com/PostHog/posthog-flutter/pull/74)
- Added iOS support for the `DEBUG` config [#74](https://github.com/PostHog/posthog-flutter/pull/74)

## 4.0.0-alpha.2

- Internal changes only
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [
// The PosthogObserver records screen views
// The PosthogObserver records screen views automatically
PosthogObserver(),
],
home: Scaffold(
Expand Down Expand Up @@ -88,8 +88,8 @@ Remember that the application lifecycle events won't have any special context se
</activity>
<meta-data android:name="com.posthog.posthog.API_KEY" android:value="YOUR_API_KEY_GOES_HERE" />
<meta-data android:name="com.posthog.posthog.POSTHOG_HOST" android:value="https://app.posthog.com" />
<meta-data android:name="com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS" android:value="false" />
<meta-data android:name="com.posthog.posthog.DEBUG" android:value="false" />
<meta-data android:name="com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS" android:value="true" />
<meta-data android:name="com.posthog.posthog.DEBUG" android:value="true" />
</application>
</manifest>
```
Expand All @@ -109,7 +109,9 @@ Remember that the application lifecycle events won't have any special context se
<key>com.posthog.posthog.POSTHOG_HOST</key>
<string>https://app.posthog.com</string>
<key>com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS</key>
<false/>
<true/>
<key>com.posthog.posthog.DEBUG</key>
<true/>
[...]
</dict>
</plist>
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ android {
dependencies {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
implementation 'com.posthog:posthog-android:3.0.1'
implementation 'com.posthog:posthog-android:3.1.0'
}

testOptions {
Expand Down
10 changes: 1 addition & 9 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,9 @@
android:value="https://app.posthog.com" />
<meta-data
android:name="com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS"
android:value="false" />
android:value="true" />
<meta-data
android:name="com.posthog.posthog.DEBUG"
android:value="true" />
<!--
If you want to debug the plugin events. Default is false.
<meta-data android:name="com.posthog.posthog.DEBUG" android:value="true" />

Expected shell output (flutter run) when this flag is enabled:
D/Analytics(32684): Ran TrackPayload{event="ButtonClicked"} on integration Posthog.com in 480110 ns.
D/Analytics(32684): Ran TrackPayload{event="ButtonClicked"} on integration Posthog.com in 104410 ns.
-->
</application>
</manifest>
4 changes: 3 additions & 1 deletion example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
<key>com.posthog.posthog.API_KEY</key>
<string>_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI</string>
<key>com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS</key>
<false/>
<true/>
<key>com.posthog.posthog.DEBUG</key>
<true/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
Expand Down
6 changes: 5 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [
// The PosthogObserver records screen views automatically
PosthogObserver()
],
home: Scaffold(
appBar: AppBar(
title: const Text('PostHog Flutter App'),
Expand Down Expand Up @@ -52,7 +56,7 @@ class _MyAppState extends State<MyApp> {
"foo": "bar",
});
},
child: const Text("Capture Screen"),
child: const Text("Capture Screen manually"),
),
ElevatedButton(
onPressed: () {
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ description: Demonstrates how to use the posthog_flutter plugin.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: "none" # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=2.18.0 <4.0.0'
Expand Down
4 changes: 3 additions & 1 deletion ios/Classes/PosthogFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
}

let host = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.POSTHOG_HOST") as? String ?? PostHogConfig.defaultHost
let postHogCaptureLifecyleEvents = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS") as? Bool ?? false
let postHogCaptureLifecyleEvents = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS") as? Bool ?? false
let postHogDebug = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.DEBUG") as? Bool ?? false

let config = PostHogConfig(
apiKey: apiKey,
host: host
)
config.captureApplicationLifecycleEvents = postHogCaptureLifecyleEvents
config.debug = postHogDebug
config.captureScreenViews = false

// Update SDK name and version
Expand Down
15 changes: 8 additions & 7 deletions lib/src/posthog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@ class Posthog {
required String eventName,
Map<String, Object>? properties,
}) {
final propertiesCopy = properties == null ? null : {...properties};

final currentScreen = _currentScreen;
if (properties != null &&
!properties.containsKey('\$screen_name') &&
if (propertiesCopy != null &&
!propertiesCopy.containsKey('\$screen_name') &&
currentScreen != null) {
properties['\$screen_name'] = currentScreen;
propertiesCopy['\$screen_name'] = currentScreen;
}
return _posthog.capture(
eventName: eventName,
properties: properties,
properties: propertiesCopy,
);
}

Future<void> screen({
required String screenName,
Map<String, Object>? properties,
}) {
if (screenName != '/') {
_currentScreen = screenName;
}
_currentScreen = screenName;

return _posthog.screen(
screenName: screenName,
properties: properties,
Expand Down
7 changes: 6 additions & 1 deletion lib/src/posthog_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ class PosthogObserver extends RouteObserver<PageRoute<dynamic>> {
final ScreenNameExtractor _nameExtractor;

void _sendScreenView(PageRoute<dynamic> route) {
final String? screenName = _nameExtractor(route.settings);
String? screenName = _nameExtractor(route.settings);
if (screenName != null) {
// if the screen name is the root route, we send it as root ("/") instead of only "/"
if (screenName == '/') {
screenName = 'root (\'/\')';
}

Posthog().screen(screenName: screenName);
}
}
Expand Down