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

SuperProperties are not registered during init before the automatic events are tracked. #658

Open
gulcebasar opened this issue Dec 16, 2024 · 7 comments

Comments

@gulcebasar
Copy link

gulcebasar commented Dec 16, 2024

I am trying to use

open class func initialize(
   token apiToken: String,
   trackAutomaticEvents: Bool,
   flushInterval: Double = 60,
   instanceName: String? = nil,
   optOutTrackingByDefault: Bool = false,
   useUniqueDistinctId: Bool = false,
   superProperties: Properties? = nil,
   serverURL: String? = nil
) -> MixpanelInstance

to register super properties before tracking of the automatic events

but since registerSuperProperties is async automaticEvents.initializeEvents(instanceName: self.name) is run before super properties are set.

Is it possible to fix this issue?

@jaredmixpanel
Copy link
Contributor

jaredmixpanel commented Dec 16, 2024

@gulcebasar I'm not able to repro. Make sure you are using the latest version of the SDK: v4.3.0.

registerSuperProperties dispatches the task to update the super properties to the trackingQueue. This is the same queue that track event tasks are dispatched to, so the super properties update task will be executed prior to the track event task.

https://github.com/mixpanel/mixpanel-swift/blob/master/Sources/MixpanelInstance.swift#L1303-L1304

https://github.com/mixpanel/mixpanel-swift/blob/master/Sources/MixpanelInstance.swift#L1059-L1066

with this code:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        Mixpanel.initialize(token: "MY_TOKEN", trackAutomaticEvents: true, superProperties: ["init_prop": "1.0"])

I can see init_prop: 1.0 on First App Open and App Session automatic events:

Image

Image

@gulcebasar
Copy link
Author

Hello @jaredmixpanel can you please try it with

Mixpanel.initialize(token: "MY_TOKEN", trackAutomaticEvents: true, optOutTrackingByDefault: true, superProperties: ["init_prop": "1.0"])

I checked it again and I think the issue might be caused by
https://github.com/mixpanel/mixpanel-swift/blob/61ce9b40817466fb1334db1d7a582fbaf616ab4c/Sources/Track.swift#L89C9-L91C10
but I am not sure.

Super properties are not registered when optOutTrackingByDefault: true

@jaredmixpanel
Copy link
Contributor

jaredmixpanel commented Dec 17, 2024

@gulcebasar
Copy link
Author

gulcebasar commented Dec 18, 2024

Ok so my current flow is as follows:

mixpanelInstance = Mixpanel.initialize(token: "MY_TOKEN", trackAutomaticEvents: true, optOutTrackingByDefault: true, superProperties: ["init_prop": "1.0"])

then after opt in I have

mixpanelInstance?.optInTracking()
mixpanelInstance?.registerSuperPropertiesOnce(["init_prop": "1.0"])

and my tracked events are

First App Open -> no init_prop
Opt In -> no init_prop
Any event after -> has init_prop

OR

I could possibly do

mixpanelInstance?.optInTracking(properties: ["init_prop": "1.0"])
mixpanelInstance?.registerSuperPropertiesOnce(["init_prop": "1.0"])

then I would have

First App Open -> no init_prop
Opt In -> has init_prop
Any event after -> has init_prop

But currently, due to this if

if mixpanelInstance?.hasOptedOutTracking() ?? false {

it is not possible to register super properties before opt-in
and there is no way to add a super property to the tracking of First App Open while using optOutTrackingByDefault: true

I would like to have a way of adding my super properties also to First App Open. I am open for any solution, but I would assume using init with superProperties should make this possible.

@jaredmixpanel
Copy link
Contributor

@gulcebasar I see, I see... you've actually found a different bug.

        Mixpanel.initialize(token: "MY_TOKEN", trackAutomaticEvents: true, optOutTrackingByDefault: true, superProperties: ["init_prop": "1.0"])
        Mixpanel.mainInstance().optInTracking(properties: ["init_prop": "1.0"])
        Mixpanel.mainInstance().registerSuperPropertiesOnce(["init_prop": "1.0"])

This sequence should NOT produce a First App Open event because at the time that event is tracked the user is opted out.

The reason it does get tracked is because we check the opt out status outside of the tracking queue, but the opt out update is inside the tracking queue, so we end up checking the status before the update has been processed.

https://github.com/mixpanel/mixpanel-swift/blob/master/Sources/MixpanelInstance.swift#L1059-L1061

https://github.com/mixpanel/mixpanel-swift/blob/master/Sources/MixpanelInstance.swift#L1496-L1520

If a user is opted out, the Mixpanel SDK should not be tracking anything. There should not be ANY tracked events with timestamps prior to the Opt In event. Initializing with optOutTrackingByDefault: true should be incompatible with automatic First App Open tracking. So I will need to fix this.

If you still want to capture data about your users while they are opted out, you'll need to implement your own First App Open tracking mechanism, which is fairly trivial to do. You can see how we do it using UserDefaults: https://github.com/mixpanel/mixpanel-swift/blob/master/Sources/AutomaticEvents.swift#L52-L62

So you'd just need to set a similar key:value pair in your own UserDefaults suite (maybe instead of bool, store the date) and then after you call optInTracking check for that key and then track your First App Open event. But the Mixpanel SDK should not be doing this.

@gulcebasar
Copy link
Author

Thank you, First App Open not being tracked before opt-in would already solve my issue.
Then I will be waiting for the update :)

@jaredmixpanel
Copy link
Contributor

@gulcebasar For now, you can do...

Mixpanel.initialize(token: "MY_TOKEN", trackAutomaticEvents: false, optOutTrackingByDefault: true)
Mixpanel.mainInstance().optInTracking(properties: ["init_prop": "1.0"])
Mixpanel.mainInstance().registerSuperPropertiesOnce(["init_prop": "1.0"])
Mixpanel.mainInstance().trackAutomaticEventsEnabled = true

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

No branches or pull requests

2 participants