cmackay / google-analytics-plugin

Cordova Google Analytics Plugin for Android & iOS
Apache License 2.0
89 stars 43 forks source link

iOS: sendHitsInBackground ability? #74

Open jvjvjv opened 7 years ago

jvjvjv commented 7 years ago

Hey this is an enhancement request. Google's default dispatch interval is two minutes. But it looks like if a user closes the app before that two minutes, the data won't be dispatched.

jvjvjv commented 7 years ago

I also wanted to request manual dispatch capability, but I wasn't sure if I should make it a separate ticket. I apologise for immediately unloading this onto you instead of implementing it myself and then sending you a pull request.

petarov commented 7 years ago

@jvjvjv About your first question - yes, that seems to be the case as written in the GA docs.

By default, data is dispatched from the Google Analytics SDK for iOS every 2 minutes.

If a user loses network access or quits your app while there are still hits waiting to be dispatched, those hits are persisted in local storage. They will be dispatched the next time your app is running and dispatch is called.

So the hits should be dispatched once the user opens the app again.

There are currently 3 dispatch modes - periodic, manual and background. I think this plugin only supports periodic dispatch. Manual seems easy to implement, background looks also possible, but I haven't tried it myself so far.

cmackay commented 7 years ago

@petarov Thanks for the info. Yeah I just also looked at both the Android and iOS APIs and those seem pretty straight forward to implement. We could probably add setLocalDispatchPeriod and dispatchLocalHits pretty easily. I'll can add them tomorrow if we want. :)

cmackay commented 7 years ago

Just realized setDispatchInterval is already implemented. Forgot about that. :) But I am adding a dispatchHits() function which can be used to manually trigger dispatching.

cmackay commented 7 years ago

I added the dispatchHits functions and I should have it released sometime this weekend. #75 Thanks for adding the feature request. This will allow you manually dispatch the hits. The background dispatching requires additional permissions WAKE_LOCK and I am not sure I would want to add that as part of this plugin since it would apply to all users. I am open to suggestions if there are other ideas. Thanks!

Here are the dispatch related docs: https://developers.google.com/analytics/devguides/collection/android/v4/dispatch https://developers.google.com/analytics/devguides/collection/ios/v3/dispatch

petarov commented 7 years ago

@cmackay Good points about WAKE_LOCK! This is something I've been wondering as well. Perhaps we can solve this by using Cordova hooks and only add dependencies when users specify they need them. This might also be a solution for optional IDFA dependencies. Will have a look when I have some time.

cmackay commented 7 years ago

I published the release 1.0.2 to npm which contains the dispatchHits function. I guess I will leave this issue open for the related investigation of background dispatching. Let me know if there are any questions or additional ideas for the plugin. Thanks!