google-analytics-plugin icon indicating copy to clipboard operation
google-analytics-plugin copied to clipboard

Using web site data tracker for mobile app

Open premedios opened this issue 5 years ago • 7 comments

Why is this plugin forcing people to create a web site data property to get a tracking number when cordova is supposed to be generating mobile apps and not responsive web sites?

premedios avatar Sep 06 '19 10:09 premedios

Hi,

Mainly because google no longer supports the Google Analytics Services SDK's after October 2019, more info here https://support.google.com/firebase/answer/9167112?hl=en This plugin works with the Google Analytics SDK.

When you use this plugin, you can set up your property as an app in Google Analytics. However because support by Google will soon end, this plugin will not be useful after October 2019 (see above).

Instead you can track your cordova app just like a website, without the need for this plugin. So currently many people are pointing this out in the discussions related to this plugin. This way people can continue to track the same kind of information, without making to much changes in their existing applications.

Alternatively you can make use of the Firebase SDK, this way you can set up your property as an app (like recommended by Google). However:

  • less tracking information is available in Firebase analytics compared with Google Analytics
  • if you are using Phonegap Build, I don't think there currently is an (easy) way to include any Firebase plugin, because Phonegap Build does not support hooks

Kind regards, Michiel

MichielJ87 avatar Sep 16 '19 13:09 MichielJ87

Hi there, @victorsosa so unfortunately this plugin has become obsolete, right?

xcafebabe avatar Nov 29 '19 19:11 xcafebabe

For sometime the GA still be supporting this api; except that there is no more SDK development of the API. @xcafebabe I still using the GA website (mobile version) and collecting data.

victorsosa avatar Nov 29 '19 19:11 victorsosa

Well I understand that there is not much interest to show how we can continue using the plugin.

Anyway, thank you for keeping the project alive. Cheers.

xcafebabe avatar Nov 29 '19 22:11 xcafebabe

Hello @MichielJ87

Instead you can track your cordova app just like a website, without the need for this plugin.

Really? Is it simple as this? Could you give us more details about how to make this works?

I would like to use Google Analytics in my Cordova app. Informations found about it after doing researches are a bit blurry to me for now (SDK deprecated, Firebase, etc.). So if finally we don't need plugin at all this would be really simple to integrate.

On my Google Analytics account, I have created a new test account to try to track my Cordova App. Then I put the tracking code given by GA into my index.html file (just after the head tag).

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
   window.dataLayer = window.dataLayer || [];
   function gtag(){dataLayer.push(arguments);}
   gtag('js', new Date());
   gtag('config', 'UA-XXXXXXXXX-X');
</script>

When I run my Cordova app on my local devices (iOS and Android) nothing happen on the "Real time" tab on my GA dashboard.

So I tried to include the tracking code on a test.html file on one of my server and the "Real time" tab on GA increased to 1 user immediately. So the tracking code works.

So I'm curious about how you do to get a "website tracking code" working in your Cordova apps... Any advises?

Thanks!

Faksprod avatar Jan 04 '20 06:01 Faksprod

Hi @Faksprod This is what I currently have in my html head:

`

// tracking script from Google (using ga.js, and not gtag.js...)
(function (i, s, o, g, r, a, m) {
  i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
    (i[r].q = i[r].q || []).push(arguments)
  }, i[r].l = 1 * new Date(); a = s.createElement(o),
    m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'js/analytics.js', 'ga'); /* local copy from web 30/08/19 https://www.google-analytics.com/analytics.js */

// THIS IS FOR LOCALSTORAGE (info from https://stackoverflow.com/questions/11026916/how-to-use-google-analytics-with-phonegap-without-a-plugin)
var GA_LOCAL_STORAGE_KEY = 'ga:clientId';
ga('create', 'UA-xxxxxxxx-x', {
  'storage': 'none',
  'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga(function (tracker) {
  localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});

// THIS IS FOR FILE URL SUPPORT (info from https://stackoverflow.com/questions/11026916/how-to-use-google-analytics-with-phonegap-without-a-plugin)
ga('set', 'checkProtocolTask', function () { /* noop */ });

`

Other tips that I have are:

  • Make sure your code is no longer using any functions from the plugin
  • Make sure to remove the plugin from your config.xml, otherwise the 'ga' function will not work
  • When do your setup in Google Analytics, the suggested code for your property is using gtag.js, and not ga.js. I have used ga.js (see code snippet below) to be consistent with examples given in discussions on github

Kind regards, Michiel

MichielJ87 avatar Jan 04 '20 08:01 MichielJ87

Hello @MichielJ87 and thanks for your quick reply. I will try your solution and compare it with the one I found this night (which worked well). Solution found from this blog (French) works using an alternative method. If you think your method is better for some reasons feel free to tell me.

How to use Google Analytics in a Cordova without any plugin:

1 - Create a new account to get a tracking code on Google Analytics (just like you would do for a website tracking code, not for a mobile app)

2 - Download the Analytics.js script from Google (doc)

3 - Include the Analytics.js file to your project into a classic JS script tag. BUT FIRST, you have to find and comment the following line in the Analytics.js file.

if("http:"!=a&&"https:"!=a)throw"abort";

4 - Wait for the DeviceReady event and create your GA tracking object.

ga('create', 'UA-XXXXXX-XX', {
    'storage': 'none',
    'clientId':device.uuid
 });

5 - Then you can send any informations you want to GA to track your app.

ga('set', {
    'appName': 'myAppName',
    'appId': 'myAppId',
    'appVersion': '1.0',
    'appInstallerId': 'myInstallerId'
});

6 - Also you can send Events to GA.

ga('send', 'screenview', {'screenName': 'Home'});
ga('send', 'event', 'video', 'started');

Hope this can help someone else!

Faksprod avatar Jan 04 '20 11:01 Faksprod