mixpanel / mixpanel-iphone

Official iOS (Objective-C) Tracking Library for Mixpanel Analytics
http://mixpanel.com
Apache License 2.0
1.04k stars 566 forks source link

Calling `MPTweakValue` inline helper in swift #229

Closed samirahmed closed 7 years ago

samirahmed commented 9 years ago

The inline helper syntax conflicts with the MPTweakValue type constructor syntax ... As far as I can tell this cannot be called in swift - you have to create a objc file just to call this helper

Would be nice if you gave it a different name or named the typed differently ... or if there is a way to call it from swift please let me know :smile:

Pasta commented 9 years ago

+1

arittr commented 9 years ago

hey @samirahmed, @Pasta - you're absolutely right that you cannot define MPTweakValue in Swift. MPTweaks are constructed with a Preprocessor Macro, and we're working to move this to a Swift function so everything is compatible and usable in a Swift project. this should be coming in a future release - for now, Tweaks need to be defined in an objc file just as you've found. sorry to be the bringer of bad news here!

bradfol commented 9 years ago

@arittr Would also like to use tweaks in a swift project. Don't quite understand the current workaround. Can you elaborate? Thanks!

arittr commented 9 years ago

@bradfol we're working on moving our tweaks code to a Swift function - for now, you can only use MPTweak/TweakValue in an Objective-C class. you can have Obj-C and Swift classes side by side in a project, but Tweaks cannot be used in Swift classes right now. i'll close/update this issue when the new release is done. thanks!

bradfol commented 9 years ago

@arittr Thanks for clarifying. Hope to see some updates for Swift soon!

john-langley commented 9 years ago

+1

sfader commented 9 years ago

+1

ohad7 commented 9 years ago

Managed to get this to work. The trick is to use the MPTweakStore directly. Here's the gist :

https://gist.github.com/ohad7/8ce12a432773fe3b1bf3

Hope this helps.

arittr commented 9 years ago

@ohad7 this is super cool, thanks so much for sharing! i'm going to test this out today.

ohad7 commented 9 years ago

Thanks! I got to test it a bit more and make a few changes, please take a look. I'm now relying on Mixpanel to refresh the experiments every time the app goes to background (instead of doing it myself every 30 mintues), and I'm persisting the tweaks to the disk since Mixpanel won't give you the same experiment twice. Will update in case I find bugs etc.

arittr commented 9 years ago

@ohad7 sounds great - I'll take another look today. FWIW variants/experiments will persist in /decide - so in effect Mixpanel will serve the experiment every time the app is opened (or /decide is manually polled). that being said caching is a great idea and something our library does anyway.

you might want to consider a PRQ / forking the lib - I know several people who are interested in something like this. thanks again for being awesome!

holgersindbaek commented 9 years ago

What's the status on this? Half a year after and it's still impossible to use Mixpanels Tweaks in a Swift app, unless you include @ohad7's file?!

gravera commented 9 years ago

+1

kareemk commented 9 years ago

+1

samirahmed commented 9 years ago

its been 8 months and mixpanel hasn't answered ... anybody still waiting on a fix should just bridge the gap with the wrapped call to an objc method that just makes this one call

reabbotted commented 8 years ago

Is it just me or is it not totally bonkers that this doesn't work out of the box?

samgreen commented 8 years ago

We're planning on pushing a release this week that will resolve this issue. Thanks for your patience.

holgersindbaek commented 8 years ago

@samgreen How's the release coming along?

vandutran commented 8 years ago

It seems that this has not been fixed in 2.8.3 as I am using that version and I still get an error with Swift.

holgersindbaek commented 8 years ago

@samgreen Can we get an update from MixPanel?

samgreen commented 8 years ago

@holgersindbaek Absolutely, sorry for the delay on this. It's not such a simple change as was linked in the gist above. We released v2.9.0 on Friday and this has laid the groundwork for future Swift support.

Unfortunately the mechanism by which we read all of the tweaks from the binary (seen here: https://github.com/mixpanel/mixpanel-iphone/blob/bdd0f5a7f33ea0bfc4193f84ae2ed80258215bab/Mixpanel/MPTweakInline.m#L127), is not compatible with the way Swift only projects are compiled. It appears to us that the Swift compiler is not only mangling symbols and names, but rearranging where things are placed in the Mach-O header. The way we accomplish the magic for tweaks relies heavily on this concept.

We are reviewing possible solutions at this time, but we would love any input from our community on this one.

That said, if you use the gist above, the tweaks will function correctly, but you will only be able to see them on the website when you have that specific view / view controller loaded. This is a non-optimal solution, and definitely not one that we wanted to ship to our users.

samgreen commented 8 years ago

Hopefully #364 will resolve this issue. Thanks for your patience everyone.

vandutran commented 8 years ago

"This is a non-optimal solution, and definitely not one that we wanted to ship to our users."

Any new updates Sam? Thanks.

samgreen commented 8 years ago

Hi @vandutran, thanks for checking in. At this point the only problem we face is the lack of the __attribute LLVM keyword in swift. We use this keyword to locate the tweaks by name in the application binary. I am currently investigating using global static variables so we would be able to locate those instead, but we are running in to issues with Swift's name mangling.

We care deeply about this issue and are hard at work restoring this magical feature for Swift. Thanks so much for your continued patience.

mackuba commented 8 years ago

So the problem is that you're using some dark magic to extract the list of all tweak names used in MPTweakLabel() calls, and you can't do that for Swift because compiled Swift code looks different than compiled ObjC code, did I get that right?

mackuba commented 8 years ago

I've added the call in an ObjC file as a workaround, but the tweaks still don't appear in the A/B builder on the site, am I doing something wrong? Or is it that if the app includes any Swift at all, the tweaks don't work, even if they're in an ObjC file?

cph2117 commented 8 years ago

I've tried the same thing, @mackuba , and likewise the tweaks aren't appearing in A/B builder. I've emailed them, so I will let you know what they say.

SRandazzo commented 8 years ago

just wanted to get a 2-week check in for any updates on this issue, @cph2117 have you heard back?

cc @samgreen

Thanks!

shnktt commented 8 years ago

I asked and got feedback.

Tweaks are not compatible with Swift yet.
This is a known issue: https://github.com/mixpanel/mixpanel-iphone/issues/229
I would recommend following that issue on Github so that you automatically get updates.
Sorry I don't have better news at the moment, but we are working on this, I'm just not certain when it will be available.

hmm....

avive commented 8 years ago

+1

cph2117 commented 8 years ago

@SRandazzo sorry for the huge delay, I didn't see your question.

We ended up scrapping this and using Optimizely. So now we have Optimizely for AB testing and Mixpanel for event tracking.

henryeverett commented 8 years ago

Any progress on this yet?

pavlibeis commented 8 years ago

Hello guys, any news ??

holgersindbaek commented 8 years ago

@samgreen Still no timeline for this?

holgersindbaek commented 8 years ago

??

yarneo commented 8 years ago

I just want to point out that it is definitely possible to use MPTweakValue in a Swift project, you would just have to use a bridging header and write your tweaks in Objective-C. You can read more about this here: https://mixpanel.com/help/reference/ios#tweaks under the "Swift" section.

chrisvxd commented 8 years ago

@yarneo whilst I believe most people here are aware of that solution (see thread), I think the point of this issue is to request a native swift solution rather than having to use objective C and a bridging header as a clunky and half-baked workaround.

yarneo commented 8 years ago

@chrisvxd Thanks for the comment. The reason I am pointing it out, is that some people are not aware it is possible and I wanted to clarify. Rest assured that once we have a solution we will update this thread.

Fawxy commented 8 years ago

Losing hope :(

pasevin commented 7 years ago

Still a deal breaker for us...

xManson commented 7 years ago

found this https://mixpanel.com/help/reference/swift

yarneo commented 7 years ago

As @xManson mentioned, we now have a Swift library that also supports A/B testing and tweaks. The mechanism is different but you can see how to initialize and use the tweaks here: https://mixpanel.com/help/reference/swift#tweaks

pasevin commented 7 years ago

Great news!

yarneo commented 7 years ago

Closing this issue. If someone wishes to use Tweaks in Swift, then please use our Swift library!