mixpanel / mixpanel-js

Official Mixpanel JavaScript Client Library
https://mixpanel.com/help/reference/javascript
Other
889 stars 313 forks source link

Cordova Support #24

Open sandorfr opened 9 years ago

sandorfr commented 9 years ago

Do you have plans regarding cordova support?

There are two main concerns when it comes to cordova support using mixpanel-js:

  1. The current implementation is cookie based
  2. Offline support

There is a simple workaround for the first one which consists of replacing the current cookie implementation by a localstorage based implementation. Currently this is dirty but I can work on a pull request if you are interested.

I don't have worked yet on the second issue but I think we can build a local storage solution too. I think this would be awesome for cordova but this would help people who build websites with offline support too.

tdumitrescu commented 9 years ago

Hi, thanks so much for pointing out the issues and potential solutions. Offline support and alternatives to cookie-based tracking would be awesome. Unfortunately there's nothing concrete currently on the roadmap, so pull requests are probably the quickest path to seeing the features you need. A few notes:

Let us know if you have more questions!

sandorfr commented 9 years ago

Hi, yes there is one thing. I'm struggling running tests do you have any guidance on that? My issue seems to be related with cookie_included getJson calls (which are failing with a 404).

Update : I was able to run them using a basic nodejs server. I still have some tests failing because i'm not returning the expected value. So I guess there is actually something missing from this repository to enable testing.

tdumitrescu commented 9 years ago

Apologies for that; the tests do rely on an external server resource which wasn't included when the lib was open-sourced. I've been meaning to add a little Node server to the repo to handle that. The Django code for it looks basically like:

def test_cookie_included(request, cookie_name):
    return json_response(request, 1 if cookie_name in request.COOKIES else 0)

so the response is 1 when the cookie has been set and 0 otherwise. Does that help?

sandorfr commented 9 years ago

It does help :) Actually I wrote a basic nodejs/express server which I will update with using that information. I can submit another PR for that too if you want.

tdumitrescu commented 9 years ago

That would be very welcome!

sandorfr commented 9 years ago

Just submitted a PR about the test server. I still have some work to do about localStorage to make it clean but I'm on it.

sandorfr commented 9 years ago

I've added a pull request with the localStorage alternative to cookies.

KeKs0r commented 9 years ago

@sandorfr I see that you already solved one of the two problems for good cordova support. Did you also tackle the queue topic?

sandorfr commented 9 years ago

@KeKs0r I have not yet started it.

mobidev111 commented 8 years ago

here's a queue implementation: http://meltuhamy.com/dev/offline-resilient-mixpanel-tracking-for-ionic-without-a-cordova-plugin/ https://github.com/meltuhamy/belfastsalah/blob/master/www/js/svc/mixpanel.js

@sandorfr @tdumitrescu any chance to take on this in the foreseeable future? similarly to what you did with the localstorage support?

mobidev111 commented 8 years ago

alternative approach here: https://github.com/skotzko/mixpanel-event-queue

tdumitrescu commented 8 years ago

The best way to push a feature like this forward is with a pull request, otherwise it's largely a matter of us prioritizing the request against a whole pile of other internal development work, which means a longer wait. I'm happy to discuss ideas if you want to validate an approach before submitting code.

sandorfr commented 8 years ago

Hi @mobidev111, I don't MEQ is going to be of any help when it comes to offline support because this is using in Memory storage.

I was considering implementing a queue on top of IndexedDb and then batch the events. Since reinventing the wheel is not going to help I think it might be interresting to take a look at this : https://github.com/orthecreedence/hustle.

That being said there is some work and a lot of testing to be done to get something which could be merged here, and I'm also prioritizing things on my side. So unless someone makes it a priority for me I'm very short on time atm.

jskrepnek commented 8 years ago

To add another consideration for Cordova support, it looks like in all scenarios mixpanel will default the protocol to 'http' rather than 'https', since the protocol is actually 'file'.

See: https://github.com/mixpanel/mixpanel-js/blob/40c158c0030a65df0c203a2817f917757af09666/src/mixpanel-core.js#L72

sandorfr commented 8 years ago

@jskrepnek interesting, and it looks like this is not configurable yet.

jskrepnek commented 8 years ago

@sandorfr I've successfully patched it in place, but that's far from ideal.

tdumitrescu commented 8 years ago

It is currently configurable to the extent that you can specify the entire api host string in the config, though it's certainly not ideal:

mixpanel.init('MY TOKEN', {
  api_host: 'https://api.mixpanel.com'
});

What would be the most useful means of configuration for you?

jskrepnek commented 8 years ago

@tdumitrescu That's great, thanks. I'd suggest adding that configuration option to the Javascript Full API Reference.

jskrepnek commented 8 years ago

Would it make sense to make 'https' the default, rather than 'http'? That way scenarios like cordova where the protocol is 'file' would default the safe way.

tdumitrescu commented 8 years ago

Good call, will need to add it here.

tdumitrescu commented 8 years ago

Yeah, we'll have to think about potential implications for backwards-compatibility with switching the default, though it seems safe to me off the top of my head.

jfbloom22 commented 6 years ago

I believe this issue can be closed. I am using Mixpanel in several Cordova apps at this point. If you are using bower and your app goes offline: notice the section titled: Using Bower to load the entire library which explains how to load Mixpanel without the CDN. This will allow it to pass the CSP for Chrome:extensions as well. Side note: the instructions are slightly wrong. mixpanel.js does not exist. It should be mixpanel.min.js.