5

I would like to know how to integrate ionic v1 with google analytics and to track specific screens.

For example, I want to have google analytics code UA-XXX and to track specific navigation (tab1, tab2, etc)

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
Axil
  • 3,606
  • 10
  • 62
  • 136

1 Answers1

1

This question is too vast so I am not going to cover each and every detail but here is how you can integrate Google Analytics to your ionic v1 app's Screens/Tabs.

For this two things are important, first is life cycle events of Ionic Views and second is Google Analytics's cordova-plugin-google-analytics.

For each Tab there should be one controller and in that controller add the Ion View's life cycle events as per your tracking requirement. Below are the available events:

FIRST TIME VIEW INITIALIZATION

View 1 – loaded
View 1 – beforeEnter
View 1 – enter
View 1 – afterEnter

TRANSITION FROM ONE VIEW TO ANOTHER

View 2 – loaded
View 2 – beforeEnter
View 1 – beforeLeave
View 2 – enter
View 1 – leave
View 2 – afterEnter
View 1 – afterLeave

But I think you may meed only below two events mainly, enter and leave:

$scope.$on('$ionicView.enter', function(){
  // Your Google Analytic event code of your choice
  window.ga.startTrackerWithId('UA-XXXX-YY', 30);
});

$scope.$on('$ionicView.leave', function(){
  // Your Google Analytic event code of your choice
  window.ga.startTrackerWithId('UA-XXXX-YY', 30);
});

References:

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • tqvm, i'll have a look this weekend – Axil Jun 09 '18 at 05:14
  • I cant get it to work, can you please check the code and output: https://dpaste.de/FT1P. tqvm. It has this error: TypeError: Cannot read property 'startTrackerWithId' of undefined – Axil Jun 09 '18 at 16:25
  • @Axil please read the documentation carefully. There is no `$cordovaGoogleAnalytics`. Why did you add this and why are you calling like this `$cordovaGoogleAnalytics.startTrackerWithId('UA-79154745-1');`. Check the documentation again and call like this `window.ga.startTrackerWithId('UA--79154745-1', 30);` – Vikasdeep Singh Jun 10 '18 at 04:22
  • i tried doing window.ga.startTrackerWithId('UA--79154745-1', 30); and it was same problem - Cannot read property 'startTrackerWithId' of undefined. Have you tried this before ? Also I'ved only tried with browser, not on device – Axil Jun 10 '18 at 04:23
  • Are you fulfilling the **Prerequisites**? – Vikasdeep Singh Jun 10 '18 at 04:28
  • Im not clear of the Prerequites https://www.npmjs.com/package/cordova-plugin-google-analytics#javascript-usage this says that Google Analytics SDK is to be installed. https://ionicframework.com/docs/native/google-analytics/ which is the same looks pretty straightforward. How do I check the plugin version since there is release notes for v1.0.0, v1.7.x, v1.7.11 but from config.xml/package-lock.json it shows 0.1.14 – Axil Jun 10 '18 at 04:48