Giter VIP home page Giter VIP logo

posthog-android's Introduction

posthog-android's People

Contributors

alexkim205 avatar bmarty avatar dependabot[bot] avatar edscode avatar fuziontech avatar hazzadous avatar jimexist avatar liyiy avatar marandaneto avatar mariusandra avatar yakkomajuri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

posthog-android's Issues

[Question] Expected behaviour when identifying whilst opt'd out

Hello!

What's the expected behaviour when calling posthog.identify whilst the posthog client is opt'd out?
I can see that the propertiesCache contains the properties set whilst the client was opt'd out but they're never sent.

posthog.optOut(true)
posthog.identify(properties)
posthog.optOut(false)
posthog.identify(id)

PostHog Android client still using support libraries. Need to use AndroidX

When building a project I see these warning in the logs:

"The following legacy support libraries are detected:\n:app:debugRuntimeClasspath -> com.posthog.android:posthog:1.1.2 -> com.android.support:support-annotations:28.0.0"

This behavior will not be allowed in Android Gradle plugin 8.0."

Would be nice to update dependencies client library is using.

`identify` should not allow already identified users

Description

Relates to PostHog/posthog-js#524

const isKnownAnonymous = this.persistence.get_user_state() === 'anonymous' is the check where we know if calling identify again is allowed or not.
If the user is already identified, calling identify again isn't allowed, if that's intentional, you should call alias instead.

If you want to identify another user again, you have to call reset first.
https://posthog.com/docs/product-analytics/identify#3-reset-after-logout

There are more restrictions described here.

By not doing this, we might be merging users wrongly and we'll likely get a support request to split persons.

This has to be done on the other SDKs as well such as:

  • iOS
  • RN (js-lite)
  • ...

Use uuidv7 instead of uuidv4

Description

  • Android
  • iOS
  • RN

After the migration, events from older versions of the SDKs won't work when using Session properties such as Session durations (Part of the new Mobile dashboard template).
Web analytics need to migrate from v4 to v7 to speed up a few things:

@robbie-c It pretty much is a must, as it allows sampling by session ID, and even without sampling it's about 2x faster. I can't really release web analytics without this change

So we need to fit this within this sprint or latest the beginning of the next sprint before Web Analytics does the migration and breaks the current version of the SDKs

Build error on project using kotlin version 1.7.10

Description

implementation("com.posthog:posthog-android:3.+") is causing the build error this :.gradle/caches/transforms-3/2e83842a3dca5618ece679a149e8fbde/transformed/jetified-okio-jvm-3.6.0.jar!/META-INF/okio.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.

Adds referrerURL automatically

Description

Context ueman/flutter_referrer_info#2

Android already listens to ActivityLifecycleCallbacks so it'd be easier to add the referrerURL reading the intent Intent.EXTRA_REFERRER, Intent.EXTRA_REFERRER_NAME and or com.android.browser.application_id.

Prop $referrer, and $referring_domain

  • Android
  • iOS
  • RN
  • Flutter

shouldSendFeatureFlags can make lose events

In PostHog.java:479

if (shouldSendFeatureFlags) {
  ValueMap flags = featureFlags.getFlagVariants();
  List<String> activeFlags = featureFlags.getFlags();

An exception happens if PostHogFeatureFlags.reloadFeatureFlagsRequest() did not finish when featureFlags.getFlags() was called.
Although this does not make my app crash, it makes lose my very early events before reloadFeatureFlagsRequest() finishes.

java.lang.InstantiationError crash in prod during posthog init

Version

3.1.1

Steps to Reproduce

This is firebase carshlytics report from a production app. It occasionally fails on startup due to the posthog crash. For a certain user it crashed 4 times in a row on this error

Android 10
Model:A5 2020
Brand:Oppo

Expected Result

Not crash

Actual Result

  Fatal Exception: java.lang.InstantiationError: F8.c
   at com.posthog.PostHog.setup(PostHog.java:66)
   at com.posthog.PostHog$Companion.setup(PostHog.java:648)
   at com.posthog.android.PostHogAndroid$Companion.setup(PostHogAndroid.java:42)
   at com.evelize.teleprompter.TeleprompterApplication.initPostHog(TeleprompterApplication.kt:128)
   at com.evelize.teleprompter.TeleprompterApplication.onCreate(TeleprompterApplication.kt:57)
   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1199)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6913)
   at android.app.ActivityThread.access$1600(ActivityThread.java:240)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
   at android.os.Handler.dispatchMessage(Handler.java:107)
   at android.os.Looper.loop(Looper.java:241)
   at android.app.ActivityThread.main(ActivityThread.java:7888)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:512)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998)

Stack trace points here, that leaves me fairly confused

Screenshot 2024-05-27 at 14 39 40

The way I call it from my code is:
Screenshot 2024-05-27 at 14 40 55

Set `$screen_name` to all events

Description

  • Android
  • iOS
  • Flutter (when calling screen via PosthogObserver or manually)
  • RN (when calling screen via wix, react-navigation or manually)

Crash with recordScreenViews()

Version used: 2.0.3

When enabling the screen view recording, we encountered the following crash:

FATAL EXCEPTION: main
Process: at.beeone.george.dev, PID: 2802
java.lang.IllegalArgumentException: name must be provided.
	at com.posthog.android.PostHog.screen(PostHog.java:523)
	at com.posthog.android.PostHog.screen(PostHog.java:501)
	at com.posthog.android.PostHog.recordScreenViews(PostHog.java:316)
	at com.posthog.android.PostHogActivityLifecycleCallbacks.onActivityStarted(PostHogActivityLifecycleCallbacks.java:106)
	at android.app.Application.dispatchActivityStarted(Application.java:421)
	at android.app.Activity.dispatchActivityStarted(Activity.java:1407)
	at android.app.Activity.onStart(Activity.java:1948)
	at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:345)
	at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:251)
        ...

We have some activities that deliberately set android:label="" to avoid being read by screen reader that are probably causing this. The sdk should not crash. Either not tracking the event or tracking the activity class name instead would be better.

Exception java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again

Description

Exception java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
at android.view.ViewTreeObserver.checkIsAlive (ViewTreeObserver.java:984)
at android.view.ViewTreeObserver.removeOnDrawListener (ViewTreeObserver.java:759)
at com.posthog.android.replay.PostHogReplayIntegration.cleanSessionState$lambda$6$lambda$5 (PostHogReplayIntegration.kt:253)
at com.posthog.android.replay.PostHogReplayIntegration.$r8$lambda$Fo0KroR9KoezO0ojDFIMQW4ICps
at com.posthog.android.replay.PostHogReplayIntegration$$ExternalSyntheticLambda2.run
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8663)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

Tried to access visual service WindowManager from a non-visual Context

This error happens when start the service of posthog

Tried to access visual service WindowManager from a non-visual Context:id.app.App@9b44ad0 WindowManager should be accessed from Activity or other visual Context. 
Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen.
java.lang.IllegalAccessException: Tried to access visual service WindowManager from a non-visual Context:id.app.App@9b44ad0
at android.app.ContextImpl.getSystemService(ContextImpl.java:2070)
at android.content.ContextWrapper.getSystemService(ContextWrapper.java:869)
at com.posthog.android.internal.Utils.getSystemService(Utils.java:153)
at com.posthog.android.PostHogContext.putScreen(PostHogContext.java:251)
at com.posthog.android.PostHogContext.create(PostHogContext.java:132)
at com.posthog.android.PostHog$Builder.build(PostHog.java:1015)
at id.app.data.services.trackers.PostHogTrackerService.startService(PostHogTrackerService.kt:19)
at id.app.App.startTrackerService(App.kt:63)
at id.app.App.onCreate(App.kt:56)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1225)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7069)
at android.app.ActivityThread.access$1800(ActivityThread.java:258)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2207)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:344)
at android.app.ActivityThread.main(ActivityThread.java:8204)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071).

This error happens same when use the context from Activity

Kotlin Multiplatform Library

Integrating Posthog in Kotlin multiplatform projects currently requires integrating the android and ios libraries separatly. With a multiplatform library the code for android and ios must not be duplicated and user must not themself create expect/actual declarations for PostHog. The kotlin library can also be used nativly in android only projects.

Client SDKs should cache feature flags

Description

  • When the decide API is called, update the values of the feature flags in the disk
  • Always read the cached feature flags if the decide API has not finished yet, e.g. during SDK init.
  • Consider maintaining a mirror of the feature flags in memory to avoid all the IO of reading flags every time (the caching and reading should be synchronized though.)

https://github.com/PostHog/posthog-js-lite/blob/4a707fdc660785c187e0bfd99ec2df1887afcf5c/posthog-core/src/index.ts#L955-L963

Worth noting that the Android SDK v2 also didn't cache the feature flags: just in memory.

Integrate with our Feature Flags API

New contributors welcome!

Hey ๐Ÿ‘‹ are you new around here? Feel free to contribute to this issue! It is quite straightforward since there's a clear spec - no need to think about what's the best solution.

However, it isn't a teeny tiny task, which is why we're offering a $100 gift card for PostHog merch for a merged PR that implements this. You'll also be listed as a contributor on our main repo's README (4.3k stars) and get a digital contributor card.

Description

PostHog provides support for feature flags. These allow turning functionality on and off based on a response from the PostHog API.

For a client-side library like this one, we'd need an implementation similar to that of posthog-js.

Here's an overview of what implementing this entails:

  • A mechanism to send a POST request to /decide/ with the following data:

    // Content-Type: application/json
    {
        token: <posthog_project_api_key>,
        distinct_id: <user_distinct_id>
    }

    The response will contain an array/list at response['featureFlags'] with the flags that are enabled for this user. The distinct ID should be already available from existing methods implemented in this lib.
    The response should be cached and refreshed periodically.

  • A method isFeatureEnabled

    This method takes a feature flag key as an input and returns a boolean indicating if the given flag is on or off for the user based on the response from above.

  • A method reloadFeatureFlags

    This forces a refresh of the cache by calling /decide/ again.

  • A method onFeatureFlagsLoaded

    Every time the flags are reloaded, this should be called as a callback.

Questions?

Feel free to ask me anything about this! I'm also reachable on the PostHog Slack.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.