Add Google Analytics in Ionic 3 Application using Cordova and Ionic Native Plugin

Google Analytics is a tried and tested tool which helps to track user activity in a number of ways. We can get information on wide variety of categories like most pages a user entered or left, how much time they spent, from where they came, by which means they landed etc, not only this but we can now also get gender percentage and age group of people visiting.

Similar to a website it’s important to keep track of such activity in mobile applications. We can use this analytical data to optimize user interface and make better SEO strategies.

In this article, we will go through a few steps which will add the power of Google Analytics in your Ionic 3 Application. Using Google Analytics we get information of the most version of application used, the location of users, pages most used and we can also track events which are triggered by users.

Also See: Ionic 4 | Adding Google Analytics in Ionic 4 Application

Let’s begin!

First, we will create a new Ionic 3 Application with Sidebar template to test Google Analytics in with different pages.

Make sure you have the latest version of Ionic CLI installed

Create a new application

Now as our application is ready, Next we will install Google Analytics Plugin

Install the Cordova and Ionic Native plugins

How to Get Google Analytics Tracking ID for Mobile Device Tracking

You need to follow these steps to Tracking ID for mobile devices otherwise tracking will not work for trackViews method. A normal Tracking ID is only for WebPages.

Step 1) Visit Google Analytics here then Sign In

Step 2) Select “Website” fill information then click “Get Tracking ID”. If first time then you need to accept term & conditions.

Step 3) Now You will already have a property “IonicGADemo”,

Step 4) Now click on “+ Create View” to create new Track ID for mobile. Then Select “Mobile app” fill a name then click “Create View”

Step 5) Now click on “Tracking code” under property to get a Tracking ID

Use this Tracking ID in the code below and replace it with UA-XXXXXXXXX-X

Now as you have Tracking ID you can proceed with further steps

 

Add this plugin to your app’s module

open app.module.ts file then import these plugins and add in providers array

Implementation of Google Analytics Methods in Application

Next, we will add GA code in the application to track Views a user visited and we will also add an event which will be triggered by user tap.

In app.component.ts file add the following code to start tracking with the provided Tracking ID

The method startTrackerWithId will start tracking in the application and you will start seeing your device in the Google Analytics dashboard.

Add trackView method in Home and List pages or any other pages you have in the following way. Here we have a list.ts file.

Using trackView method we can send the current page in Google Analytics dashboard. We have also added trackEvent method to record tap event.

you can check more event on documentation here 

So now you can track live visitors in your application using the Google Analytics plugin in Ionic 3

4 thoughts on “Add Google Analytics in Ionic 3 Application using Cordova and Ionic Native Plugin”

    1. Hi Adam, At the time of this post, Ionic 4 was in beta phase, check my this post if you are working on Ionic version 3 it will explain ways to create app and install native plugins in Ionic’s version 3

Leave a Reply

Your email address will not be published. Required fields are marked *