Skip to content

feat(analytics): add trackScreenView and trackScreenLeave public APIs#265

Open
rahul-mixpanel wants to merge 5 commits into
mainfrom
rahulraveendran-sdk-120-add-trackscreenview-and-trackscreenleave-public-apis-to
Open

feat(analytics): add trackScreenView and trackScreenLeave public APIs#265
rahul-mixpanel wants to merge 5 commits into
mainfrom
rahulraveendran-sdk-120-add-trackscreenview-and-trackscreenleave-public-apis-to

Conversation

@rahul-mixpanel

Copy link
Copy Markdown

Summary

  • Adds mixpanel.autocapture.trackScreenView(screenName, properties?) and mixpanel.autocapture.trackScreenLeave(screenName, properties?) public APIs, mirroring the native Android/Swift SDK interfaces
  • Implements platform channel handlers for Android (getAutocapture().trackScreenView/trackScreenLeave) and iOS/macOS (autocapture.trackScreenView/trackScreenLeave)
  • Adds web fallback implementation (tracks $mp_page_view / $mp_page_leave with current_page_title and $mp_autocapture properties)
  • Bumps native SDK dependencies: Android mixpanel-android 8.7.0 → 8.9.0, iOS/macOS Mixpanel-swift 6.4.1 → 6.5.0
  • Updates example app with MixpanelNavigatorObserver for automatic screen tracking

Changes

  • lib/mixpanel_flutter.dart — New Autocapture class with lazy getter on Mixpanel
  • android/.../MixpanelFlutterPlugin.javahandleTrackScreenView and handleTrackScreenLeave methods
  • swift/Classes/SwiftMixpanelFlutterPlugin.swift — Swift handler implementations
  • lib/mixpanel_flutter_web.dart — Web fallback implementation
  • android/build.gradle — Bump mixpanel-android to 8.9.0
  • ios/mixpanel_flutter.podspec — Bump Mixpanel-swift to 6.5.0
  • macos/mixpanel_flutter.podspec — Bump Mixpanel-swift to 6.5.0
  • example/lib/main.dart — NavigatorObserver-based screen tracking

Test plan

  • All 129 unit tests pass
  • Manual test on Android — verify $mp_page_view and $mp_page_leave events fire on navigation
  • Manual test on iOS — verify events fire on navigation
  • Verify current_page_title and $mp_autocapture properties are set by native SDK (not duplicated in Dart layer)

Closes SDK-120

…ccessor

Add new Autocapture class exposing trackScreenView and trackScreenLeave
methods, matching the native Android/Swift SDK pattern
(mixpanel.autocapture.trackScreenView/trackScreenLeave).

- Dart API: new Autocapture class with lazy getter on Mixpanel
- Android plugin: delegates to mixpanel.getAutocapture()
- iOS plugin: delegates to instance.autocapture
- Web implementation: pure JS with property merging
- Bump native SDKs: Android 8.7.0→8.9.0, Swift 6.4.1→6.5.0
- Sample app: MixpanelNavigatorObserver for automatic screen tracking

Ref: SDK-120
@linear-code

linear-code Bot commented Jul 15, 2026

Copy link
Copy Markdown

SDK-120

@greptile-apps

greptile-apps Bot commented Jul 15, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

Safe to merge; all three platform paths route correctly and the Dart layer validates input before any channel call.

The implementation closely mirrors the existing People/FeatureFlags patterns. The two findings are minor: Android's missing screenName null-check is covered by the Dart-layer guard, and the unawaited futures are confined to the example app. No data-loss or crash path exists on the normal call flow.

The Android handler (MixpanelFlutterPlugin.java) and the example app's MixpanelNavigatorObserver are worth a second look.

Important Files Changed

Filename Overview
packages/mixpanel_flutter/lib/mixpanel_flutter.dart Adds Autocapture class with trackScreenView/trackScreenLeave and a lazy getter on Mixpanel; consistent with People/FeatureFlags patterns
packages/mixpanel_flutter/android/src/main/java/com/mixpanel/mixpanel_flutter/MixpanelFlutterPlugin.java Adds handleTrackScreenView/handleTrackScreenLeave; screenName is not null-checked before native call, unlike the Swift counterpart
packages/mixpanel_flutter/swift/Classes/SwiftMixpanelFlutterPlugin.swift Swift handlers use proper guard let safe-cast pattern with FlutterError on failure; consistent with rest of file
packages/mixpanel_flutter/lib/mixpanel_flutter_web.dart Web fallback correctly tracks $mp_page_view/$mp_page_leave with SDK properties taking precedence; consistent with existing handler patterns
packages/mixpanel_flutter/example/lib/main.dart Adds MixpanelNavigatorObserver for automatic screen tracking; unawaited futures in didPush/didPop silently swallow init/tracking errors
packages/mixpanel_flutter/ios/mixpanel_flutter.podspec Bumps Mixpanel-swift dependency from 6.4.1 to 6.5.0
packages/mixpanel_flutter/android/build.gradle Bumps mixpanel-android from 8.7.0 to 8.9.0

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant App as Flutter App
    participant AC as Autocapture (Dart)
    participant Ch as MethodChannel
    participant Android as Android Plugin
    participant iOS as Swift Plugin
    participant Web as Web Plugin

    App->>AC: trackScreenView(screenName)
    AC->>AC: isValidString check
    alt valid screenName
        AC->>Ch: invokeMethod("trackScreenView")
        alt Android path
            Ch->>Android: handleTrackScreenView
            Android->>Android: build JSONObject + getMergedProperties
            Android->>Android: getAutocapture().trackScreenView
            Android-->>Ch: result.success(null)
        else iOS/macOS path
            Ch->>iOS: handleTrackScreenView
            iOS->>iOS: guard screenName non-empty
            iOS->>iOS: autocapture.trackScreenView
            iOS-->>Ch: result(nil)
        else Web path
            Ch->>Web: handleTrackScreenView
            Web->>Web: merge props, SDK props win
            Web->>Web: track("$mp_page_view")
            Web-->>Ch: null
        end
        Ch-->>AC: void
    else invalid screenName
        AC-->>App: silent no-op
    end
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant App as Flutter App
    participant AC as Autocapture (Dart)
    participant Ch as MethodChannel
    participant Android as Android Plugin
    participant iOS as Swift Plugin
    participant Web as Web Plugin

    App->>AC: trackScreenView(screenName)
    AC->>AC: isValidString check
    alt valid screenName
        AC->>Ch: invokeMethod("trackScreenView")
        alt Android path
            Ch->>Android: handleTrackScreenView
            Android->>Android: build JSONObject + getMergedProperties
            Android->>Android: getAutocapture().trackScreenView
            Android-->>Ch: result.success(null)
        else iOS/macOS path
            Ch->>iOS: handleTrackScreenView
            iOS->>iOS: guard screenName non-empty
            iOS->>iOS: autocapture.trackScreenView
            iOS-->>Ch: result(nil)
        else Web path
            Ch->>Web: handleTrackScreenView
            Web->>Web: merge props, SDK props win
            Web->>Web: track("$mp_page_view")
            Web-->>Ch: null
        end
        Ch-->>AC: void
    else invalid screenName
        AC-->>App: silent no-op
    end
Loading

Reviews (5): Last reviewed commit: "fix: use guard let for screenName in Swi..." | Re-trigger Greptile

Comment thread packages/mixpanel_flutter/lib/mixpanel_flutter_web.dart
Comment thread packages/mixpanel_flutter/lib/mixpanel_flutter.dart
Add defensive null checks around getAutocapture() in trackScreenView and
trackScreenLeave to prevent potential NPE if autocapture is not initialized.
@rahul-mixpanel rahul-mixpanel changed the title Add trackScreenView and trackScreenLeave public APIs feat(analytics): add trackScreenView and trackScreenLeave public APIs Jul 16, 2026
@rahul-mixpanel
rahul-mixpanel marked this pull request as ready for review July 16, 2026 13:49
@rahul-mixpanel
rahul-mixpanel requested review from a team and jakewski July 16, 2026 13:49
…wift to 6.5.0

Replace force-unwrap (as! String) with guard let + FlutterError for
trackScreenView/trackScreenLeave to prevent crashes on missing screenName.
Bump mixpanel-swift SwiftPM dependency from 6.4.1 to 6.5.0 for autocapture support.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant