RocketChat/google-analytics-plugin

Name: google-analytics-plugin

Owner: Rocket.Chat

Description: Cordova (PhoneGap) Plugin to connect to the native Google's Universal Analytics SDK 3.0

Created: 2018-04-13 21:55:46.0

Updated: 2018-04-14 19:10:59.0

Pushed: 2018-04-14 19:10:59.0

Homepage: null

Size: 33104

Language: Objective-C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

google-analytics-plugin

npm npm MIT license

Cordova (PhoneGap) 3.0+ Plugin to connect to Google's native Universal Analytics SDK

Prerequisites:

Installing

This plugin follows the Cordova 3.0+ plugin spec, so it can be installed through the Cordova CLI in your existing Cordova project:

ova plugin add https://github.com/danwilson/google-analytics-plugin.git

This plugin is also available on npm if you are using Cordova 5.0+:

ova plugin add cordova-plugin-google-analytics

… OR the Cordova Plugin Registry if you are using a version of Cordova before 5.0 (while it remains open, as it is planning to shut down soon due to the move to npm):

ova plugin add com.danielcwilson.plugins.googleanalytics

Important Note If the latest versions (0.8.0+) of this plugin are not working for you with Android on Cordova 5.0+, please try the suggestions in Issues 123. Google Play Services has been very confusing to integrate, but in recent months it has been simplified. This plugin uses the new simpler way (including it as a framework instead of bundling it which can conflict with other plugins bundling it), but if you previously installed this plugin some old files might still be lingering.

The plugin.xml file will add the Google Analytics SDK files for Android, iOS, browser (PWA), Windows Phone 8 and/or Windows 10 (UWP). Follow Google's steps if you need to update these later. Also make sure to review the Google Analytics terms and SDK Policy

If you are not using the CLI, follow the steps in the section Installing Without the CLI

Windows Phone users have to manually add the Google Analytics SDK for Windows 8 and Windows Phone to your solution. To do this, just open your Cordova solution in Visual Studio, and add the GoogleAnalyticsSDK package via NuGet. This plugin requires v1.3.0 or higher.

Windows 10 (UWP) users have to manually add the Windows SDK for Google Analytics to your solution. To do this, just open your Cordova solution in Visual Studio, and add the UWP.SDKforGoogleAnalytics.Native package via NuGet. This plugin requires v1.5.2 or higher.

Release note

v1.0.0 – api change from `window.analyticsto ``window.ga```, 'analytics' is deprecated since 1.0.0 and you should use the new api 'ga', because in the next release we are removing the analytics.

v1.7.x – since this version there are new parameters in some of the old methods like startTrackerWithId('UA-XXXX-YY', 30) and this is causing errors for those who are using the ionic 2(ionic-native) or ionic 1 (ngCordova); these wrapper interfaces don't have the new parameters at the time we did the changes; so please update you ionic framework to the lastest version.

v1.7.11 – since this version there is back compatibility with the new and old parameters in the method startTrackerWithId('UA-XXXX-YY', 30) to avoid loading issues reported.

v1.9.0 – since this version the windows platform is supported.

JavaScript Usage

All the following methods accept optional success and error callbacks after all other available parameters.

 your 'deviceready' handler, set up your Analytics tracker:
ow.ga.startTrackerWithId('UA-XXXX-YY', 30)
ere UA-XXXX-YY is your Google Analytics Mobile App property and 30 is the dispatch period (optional)

 track a Screen (PageView):
ow.ga.trackView('Screen Title')

 track a Screen (PageView) w/ campaign details:
ow.ga.trackView('Screen Title', 'my-scheme://content/1111?utm_source=google&utm_campaign=my-campaign')

 track a Screen (PageView) and create a new session:
ow.ga.trackView('Screen Title', '', true)

 track an Event:
ow.ga.trackEvent('Category', 'Action', 'Label', Value)// Label and Value are optional, Value is numeric

 track an Event and create a new session:
ow.ga.trackEvent('Category', 'Action', 'Label', Value, true)// Label, Value and newSession are optional, Value is numeric, newSession is true/false

 track custom metrics:
rackMetric doesn't actually send a hit, it's behaving more like the addCustomDimension() method.
he metric is afterwards added to every hit (view, event, error, etc...) sent, but the defined scope of the custom metric in analytics backend
 (hit or product) will determine, at processing time, which hits are associated with the metric value.)
ow.ga.trackMetric(Key, Value) // Key and value are numeric type, Value is optional (omit value to unset metric)

 track an Exception:
ow.ga.trackException('Description', Fatal)//where Fatal is boolean

 track User Timing (App Speed):
ow.ga.trackTiming('Category', IntervalInMilliseconds, 'Variable', 'Label') // where IntervalInMilliseconds is numeric

 add a Transaction (Ecommerce) -- Deprecated on 1.9.0 will be removed on next minor version (1.10.0).
ow.ga.addTransaction('ID', 'Affiliation', Revenue, Tax, Shipping, 'Currency Code')// where Revenue, Tax, and Shipping are numeric

 add a Transaction Item (Ecommerce) -- Deprecated on 1.9.0 will be removed on next minor version (1.10.0).
ow.ga.addTransactionItem('ID', 'Name', 'SKU', 'Category', Price, Quantity, 'Currency Code')// where Price and Quantity are numeric

 add a Custom Dimension
he dimension is afterwards added to every hit (view, event, error, etc...) sent, but the defined scope of the custom dimension in analytics backend
 (hit or product) will determine, at processing time, which hits are associated with the dimension value.)
ow.ga.addCustomDimension(Key, 'Value', success, error)
y should be integer index of the dimension i.e. send `1` instead of `dimension1` for the first custom dimension you are tracking. e.g. `window.ga.addCustomDimension(1, 'Value', success, error)`
e empty string as value to unset custom dimension.

 set a UserId:
ow.ga.setUserId('my-user-id')

 set a specific app version:
ow.ga.setAppVersion('1.33.7')

 get a specific variable using this key list https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters:
r example to get campaign name:
ow.ga.getVar('cn', function(result){ console.log(result);})

 set a specific variable using this key list https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters:
r example to set session control:
ow.ga.setVar('sc', 'end', function(result){ console.log(result);})

 manually dispatch any data (this is not implemented in browser platform):
ow.ga.dispatch()

 set a anonymize Ip address:
ow.ga.setAnonymizeIp(true)

 set Opt-out:
ow.ga.setOptOut(true)

 enabling Advertising Features in Google Analytics allows you to take advantage of Remarketing, Demographics & Interests reports, and more:
ow.ga.setAllowIDFACollection(true)

nable verbose logging:
ow.ga.debugMode()
et's dry run mode on Android and Windows platform, so that all hits are only echoed back by the google analytics service and no actual hit is getting tracked!
*Android quirk**: verbose logging within javascript console is not supported. To see debug responses from analytics execute 
adb shell setprop log.tag.GAv4 DEBUG` and then `adb logcat -v time -s GAv4` to list messages 
see https://developers.google.com/android/reference/com/google/android/gms/analytics/Logger)

 enable/disable automatic reporting of uncaught exceptions
ow.ga.enableUncaughtExceptionReporting(Enable, success, error)// where Enable is boolean

Example use ionic (Ionic Native)

i --save @ionic-native/google-analytics
rt { GoogleAnalytics } from '@ionic-native/google-analytics';
rt { Platform } from 'ionic-angular';



nstructor(private ga: GoogleAnalytics, private platform: Platform) { }

itializeApp() {
this.platform.ready().then(() => {
  this.ga.startTrackerWithId('UA-00000000-0')
    .then(() => {
      console.log('Google analytics is ready now');
      //the component is ready and you can call any method here
      this.ga.debugMode();
      this.ga.setAllowIDFACollection(true);
    })
    .catch(e => console.log('Error starting GoogleAnalytics', e));      
});

Issue for using trackMetric in Ionic: currently @ionic-native/google-analytics defines the typescript signature with trackMetric(key: string, value?: any). So be aware to pass the metric index as a string formatted integer and a non empty string as a value, like window.ga.trackMetric('1', 'Value', success, error)!

Installing Without the CLI

Copy the files manually into your project and add the following to your config.xml files:

ture name="UniversalAnalytics">
aram name="ios-package" value="UniversalAnalyticsPlugin" />
ature>
ture name="UniversalAnalytics">
aram name="android-package" value="com.danielcwilson.plugins.analytics.UniversalAnalyticsPlugin" />
ature>
ture name="UniversalAnalytics">
aram name="wp-package" value="UniversalAnalyticsPlugin" />
ature>

You also will need to manually add the Google Analytics SDK files:

Integrating with Lavaca

The lavaca directory includes a component that can be added to a Lavaca project. It offers a way to use the web analytics.js when the app is running in the browser and not packaged as Cordova.

analyticsService = require('app/data/AnalyticsService');

yticsService.trackView('Home');

Browser (PWA)

For browser (PWA), people who want to use the plugin in a website that has already integrated google analytics needs to make sure that they remove the google analytics snippet from the head section of the page and change the global ga object name to something else. The plugin uses nativeGa instead. This can be changed by the following code.

nsert this in your head
ipt>
ow['GoogleAnalyticsObject'] = 'fooGa';
ript>

The plugin will pick up the new name.

Windows 10 (UWP)

The following plugin methods are (currently) not supported by the UWP.SDKforGoogleAnalytics.Native package:

Unexpected behaviour may occur on the following methods:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.