# GaPlugin with Ionic

**URL:** <https://forum.ionicframework.com/t/gaplugin-with-ionic/12273>\
**Category:** ionic-v1\
**Created:** [November 6, 2014, 11:18am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273 "2014-11-06T11:18:07Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingalb](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ingalb](https://forum.ionicframework.com/u/ingalb)\
**Post date:** [November 6, 2014, 11:18am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/1 "2014-11-06T11:18:08Z")

</div>

Hello,

I’m having problem with GaPlugin for phonegap build on Ionic app.  
I have import the GaPlugin on config.xml file:

```
<gap:plugin name="com.adobe.plugins.gaplugin" />

```

I initialize the plugin on app.js afer the Platfom ready:

```
.run(function($ionicPlatform) {
   var gaPlugin; 
   $ionicPlatform.ready(function() {
   gaPlugin = window.plugins.gaPlugin;
   gaPlugin.init(successHandler, errorHandler, "UA-xxxxxx", 10);
   });
})

```

I’m not able to get data on my Google Analytics account.  
Is this the right way to call plugins?

Thanks  
P.S. I don’t prefer to use ng-cordova lib.

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [November 6, 2014, 12:24pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/2 "2014-11-06T12:24:56Z")

</div>

And you have defined successHandler, errorHandler in your run method? in other cases you will get javascript errors.

---

<div class="post-metadata">

**Author:** ![ingalb](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ingalb](https://forum.ionicframework.com/u/ingalb)\
**Post date:** [November 6, 2014, 1:33pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/3 "2014-11-06T13:33:36Z")

</div>

Thanks @bengtler for the reply,

I change my init function adding the function on success and error :

```
gaPlugin.init(function(msg) {
                console.log("ga loaded "+msg);
            },
            function(msg) {
                console.log("ga failed "+msg);
            }, "UA-2341193-8", 10);

```

Now i’m getting successful load but I can’t see any data on Google analytics, maybe it need some time for data propagation !?

---

<div class="post-metadata">

**Author:** ![brandyshea](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/brandyshea/32/148286_2.png) [@brandyshea](https://forum.ionicframework.com/u/brandyshea)\
**Post date:** [November 6, 2014, 3:10pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/4 "2014-11-06T15:10:33Z")

</div>

I spent _days_ trying to get that plugin to work. I waited 24 hours to see if the reports would work, changed the type from a mobile app to a website, tried using different plugins (no others were supported by PhoneGap build), tried Google’s universal analytics, etc. I never got it to work. I have the same set up as you: Ionic app using PhoneGap build, and I ran into the same problems.

My solution was to set analytics to a website and use the ga.js file with GALocalStorage.js. It isn’t the ideal solution, but it works for real time and reports.

[Documentation for ga.js](https://developers.google.com/analytics/devguides/collection/gajs/) - [ga.js file](http://www.google-analytics.com/ga.js)  
[GA Local Storage](https://github.com/ggendre/GALocalStorage)  
[Stackoverflow question explaining this](http://stackoverflow.com/questions/11026916/how-to-use-google-analytics-with-phonegap-without-a-plugin)

Following this in case someone knows why the GaPlugin isn’t working. 😄

---

<div class="post-metadata">

**Author:** ![ingalb](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ingalb](https://forum.ionicframework.com/u/ingalb)\
**Post date:** [November 8, 2014, 12:10pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/5 "2014-11-08T12:10:56Z")

</div>

Hello @brandyshea,  
thanks for your comment.

I wait for 24h but the plugin don’t work and I can’t see any data on GA, then I try your solution, but this don’t function.

I tried the solution that you have post but it doesn’t work, I’m not able to view data on GA.

I have include on index.html

```
<script src="js/ga.js"></script>
<script type="text/javascript" src="js/GALocalStorage.js"></script>

```

Where I have modify ga.js  
Then after on app.js after platform.Ready :

```
ga_storage._setAccount('UA-xxxxxx-8'); //Replace with your own

```

Pls can you tell me how do you have implement this.

Thanks

---

<div class="post-metadata">

**Author:** ![brandyshea](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/brandyshea/32/148286_2.png) [@brandyshea](https://forum.ionicframework.com/u/brandyshea)\
**Post date:** [November 10, 2014, 2:20pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/6 "2014-11-10T14:20:25Z")

</div>

You are replacing the ‘UA-xxxxxx-8’ with your tracking code from google analytics, correct? Also, make sure it is set as a website in google analytics and not a mobile app.

In my app.js file after platform ready I set the account and track the page it starts on:

```
try {
    ga_storage._setAccount('UA-xxxxxx-8');
    ga_storage._trackPageview('/ticketSearch.html', 'Search Schedules');
 } catch (e) {
     console.log(e.message);
 }

```

Then each time the user changes to a different “page” (which is a state in my case) I call the trackPageview function again.

---

<div class="post-metadata">

**Author:** ![ingalb](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ingalb](https://forum.ionicframework.com/u/ingalb)\
**Post date:** [November 11, 2014, 8:40pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/7 "2014-11-11T20:40:01Z")

</div>

Hello @brandyshea,  
thanks for your reply.

I insert an old Tracking Code and now all seems to work well.  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![emaV](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@emaV](https://forum.ionicframework.com/u/emaV)\
**Post date:** [December 16, 2014, 3:05pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/8 "2014-12-16T15:05:05Z")

</div>

I found a way to use angulartics with the dan wilson [google-analytics-plugin](https://github.com/danwilson/google-analytics-plugin).

You can marge in your angulartics the [pull request](https://github.com/luisfarzati/angulartics/pull/259) from my repository.

---

<div class="post-metadata">

**Author:** ![WILDmotion](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@WILDmotion](https://forum.ionicframework.com/u/WILDmotion)\
**Post date:** [January 8, 2015, 10:42am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/9 "2015-01-08T10:42:47Z")

</div>

Hello,

Someone found a solution to integrate GaPlugin with ionic and PhoneGap Build ?

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 9, 2015, 1:07pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/10 "2015-01-09T13:07:01Z")

</div>

Jep works quite well.

Create ga account and property as mobile app.

in your config:

```
<gap:plugin name="com.adobe.plugins.gaplugin" />

```

in your .run block:

```
// add analytics for mobile tracking
        $ionicPlatform.ready(function () {
            if (window.cordova && window.plugins && window.plugins.gaPlugin) {
                window.plugins.gaPlugin.init(function () {
                    $rootScope.gaConnected = true;
                }, function () {
                    $rootScope.gaConnected = false;
                }, 'YOUR_GA_ID', 10);
            }
        });

```

In my base controller:

```
$rootScope.$on('$stateChangeSuccess', function () {
            if ($rootScope.gaConnected) {
                window.plugins.gaPlugin.trackPage(angular.noop, angular.noop, $location.path());
            }
        });

```

The listener comes from the ui-router (ionics state/rounting service) - \>i check if analytics is initialized correctly -\> i set a flag on the rootscope (see in my run-block).  
i track the current path if a state-change succeeded.

hope that helps.

---

<div class="post-metadata">

**Author:** ![WILDmotion](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@WILDmotion](https://forum.ionicframework.com/u/WILDmotion)\
**Post date:** [January 9, 2015, 3:12pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/11 "2015-01-09T15:12:14Z")

</div>

Thank you @bengtler

Meanwhile I look for another solution (with ga.js and GALocalStorage.js) and works for me.

But I would to integrate GAPlugin.  
In your solution, I don’t understand the second part (“In my base controller”).  
Do you have a simple code to only check if the application is used ? (one code in .run)

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 12, 2015, 6:50am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/12 "2015-01-12T06:50:37Z")

</div>

My apps have in most cases a “base” controller… that means like you have a site:

```
<html ng-app="app">
   <head>...</head>
   <body ng-controller="BaseCtrl">
        <ion-nav-view></ion-nav-view>
   </body>
</html>

```

my different views are loaded in the ion-nav-view.  
But all around there is one controller which handles common stuff or shared stuff for all views, like tracking the views dymanically.  
So you can write 1 code to track all views in that base controller instead of putting the tracking stuff in each view.

greets.

---

<div class="post-metadata">

**Author:** ![WILDmotion](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@WILDmotion](https://forum.ionicframework.com/u/WILDmotion)\
**Post date:** [January 12, 2015, 9:11am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/13 "2015-01-12T09:11:45Z")

</div>

Hello @bengtler,

I put :

```
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title></title>
  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
  <script src="lib/ionic/js/ionic.bundle.js"></script>
  <script src="cordova.js"></script>
  <script src="js/app.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/services.js"></script>
</head>
<body ng-app="myApp" ng-controller="BaseCtrl">
  <ion-nav-bar class="bar-calm" animation="nav-title-slide-ios7">
    <ion-nav-back-button class="button-icon icon ion-arrow-left-b">
    </ion-nav-back-button>
  </ion-nav-bar>
  <ion-nav-view animation="slide-left-right">
  </ion-nav-view>    
</body>
</html>

```

and I put in : `controllers.js`

```
angular.module('myApp.controllers', [])
.controller('BaseCtrl', function($scope, $rootScope) {
    $rootScope.$on('$stateChangeSuccess', function () {
        if ($rootScope.gaConnected) {
            window.plugins.gaPlugin.trackPage(angular.noop, angular.noop, $location.path());
        }
    });
})

```

I create ga account and Create new account “mobile app”

But I don’t have statistics.

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 12, 2015, 9:26am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/14 "2015-01-12T09:26:01Z")

</div>

check if the tracking code is called:

```
if ($rootScope.gaConnected) {
    console.log($location.path());
    window.plugins.gaPlugin.trackPage(angular.noop, angular.noop, $location.path());
}

```

then you have to switch to Realtime view in google analytics and you have to wait a little bit until tracking works.

---

<div class="post-metadata">

**Author:** ![marshman](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@marshman](https://forum.ionicframework.com/u/marshman)\
**Post date:** [January 13, 2015, 7:41pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/15 "2015-01-13T19:41:45Z")

</div>

Incase this helps, I encountered non-connecting issues as well.

I am following bengtler’s posted recommendation and using $rootScope.

**It was not until I moved the gaPlugin.init above all other code that it seemed to work fine.** I’m now wondering if somehow there may be a code conflict with another plugin.

I am using “Mobile” for my GA account tracking.

The plugin.xml uses the js-module so you do not need to include any .js files within your index.html for this plugin to work.

I am using phonegap build successfully with this plugin.

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 14, 2015, 6:45am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/16 "2015-01-14T06:45:53Z")

</div>

thats the same thing i posted?

---

<div class="post-metadata">

**Author:** ![AnilKumar852](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@AnilKumar852](https://forum.ionicframework.com/u/AnilKumar852)\
**Post date:** [October 27, 2015, 3:37pm UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/17 "2015-10-27T15:37:08Z")

</div>

what is the name Website’s URL we have to give in google analytic’s when we choose website

---

<div class="post-metadata">

**Author:** ![Austin123](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Austin123](https://forum.ionicframework.com/u/Austin123)\
**Post date:** [February 22, 2016, 8:32am UTC](https://forum.ionicframework.com/t/gaplugin-with-ionic/12273/18 "2016-02-22T08:32:25Z")

</div>

Hi, im also having a problem just like ingalb, but my code is like this:

> var gaPlugin; // Google analytics stuff
> 
> ```
> if(( /(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent) )) {
> alert('hello world! this is a device');
> document.addEventListener('deviceready', initApp, false);
> 
> ```
> 
> } else {  
> alert(‘hello world! this is NOT a device’);  
> initApp();  
> }

> /\*\*
> 
> - Init game, init google analytics  
> \*/  
> function initApp() {  
> game.state.add(‘StartMenu’, BubbleWave.StartMenu);

> // Init google analytics  
> gaPlugin = window.plugins.gaPlugin;  
> gaPlugin.init(onGASuccess, onGAError, “UA-x-x”, 10); // replace UA-x-x with your Tracking ID  
> }

I am trying to add Google Analytics in my ionic project and i included the GAPlugin, but i get this error

`Uncaught TypeError: Cannot read property 'exec' of undefined`

Anyone have a clue what might be the problem?
