Giter VIP home page Giter VIP logo

react-native-google-analytics-native's Introduction

react-native-google-analytics-native npm version

  • iOS
  • Android

Native Google Analytics for React Native. Using the Universal Analytics SDK, you can get native-level details (that are not accessible by JavaScript) while receiving analytics data, e.g.: device brand, screen resolution, OS version and more.

Installation

Android

  • Run npm install react-native-google-analytics-native --save to install using npm.

  • Add the following two lines to android/settings.gradle:

include ':react-native-google-analytics-native'
project(':react-native-google-analytics-native').projectDir = new File(settingsDir, '../node_modules/react-native-google-analytics-native/android')
  • Edit android/app/build.gradle and add the annoated lines as below:
...

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:0.15.+"
    compile project(':react-native-google-analytics-native')  // <- Add this line
}
  • Edit MainActivity.java (usually at android/app/src/main/java/com/<project-name>/MainActivity.java) and add the annoated lines as below:
import com.facebook.react.LifecycleState;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import com.reactnativega.GoogleAnalyticsPackage;                   // <- Add this line

public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {

...

        mReactRootView = new ReactRootView(this);

        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModuleName("index.android")
                .addPackage(new MainReactPackage())
                .addPackage(new GoogleAnalyticsPackage(this))      // <- Add this line
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();

...

Usage

import GoogleAnalytics from 'react-native-google-analytics-native';

// Start the tracker before tracking anything!
var enableExceptionReporting = true;
GoogleAnalytics.startTracker('UA-60031864-15', enableExceptionReporting);

// Track a Screen (PageView):
GoogleAnalytics.trackView('App Name', 'View Title');

// Track an Event:
var value = 100;
GoogleAnalytics.trackEvent('Category', 'Action', 'Label', value);

// Track an Exception:
var isFatal = false;
GoogleAnalytics.trackException('Some Bad Exception', isFatal);

// Track User Timing (App Speed):
var timeInMilliseconde = 1000
GoogleAnalytics.trackTiming('Category', timeInMilliseconde, 'Name', 'Label');

// Set User-ID
GoogleAnalytics.setUserID('user-id');

Checking Availability

If the user does not have Google Play Services on their Android device (normally happens on a rooted-device, development or rare customized device), the native Google Analytics might not work properly. But no worries! You can detect the availability of Google Play Services on the device and fallback to use any web-based Google Analytics as you need.

GoogleAnalytics.isGooglePlayServicesAvailable().then((availability) => {
  if (availability) {
    // Google Play Service and the native Google Analytics are Available!
  } else {
    // Do something else...
  }
});

Callbacks

All functions of this module supports to different types of callback, the traditional passed-in callback function, or promises. This means that you can add two optional arguments: the success callback function and the error callback function, to each function you call:

GoogleAnalytics.startTracker('UA-00000000-0', true, function(message) { console.log(message); }, function(error) { console.error(error); });

or, you can simply use the promise-thenable syntax:

GoogleAnalytics.startTracker('UA-00000000-0', true).then((message) => {
  console.log(message);
}).catch((error) => {
  console.error(error);
});

react-native-google-analytics-native's People

Contributors

zetavg avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-native-google-analytics-native's Issues

Build failed

...../node_modules/react-native-google-analytics-native/android/src/main/java/com/reactnativega/GoogleAnalyticsModule.java:6: error: cannot find symbol
import com.google.android.gms.common.GoogleApiAvailability;
                                    ^
  symbol:   class GoogleApiAvailability
  location: package com.google.android.gms.common
...../node_modules/react-native-google-analytics-native/android/src/main/java/com/reactnativega/GoogleAnalyticsModule.java:150: error: cannot find symbol
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    ^
  symbol:   class GoogleApiAvailability
  location: class GoogleAnalyticsModule
...../node_modules/react-native-google-analytics-native/android/src/main/java/com/reactnativega/GoogleAnalyticsModule.java:150: error: cannot find symbol
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
                                      ^
  symbol:   variable GoogleApiAvailability
  location: class GoogleAnalyticsModule
Note: ...../node_modules/react-native-google-analytics-native/android/src/main/java/com/reactnativega/GoogleAnalyticsModule.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
:react-native-google-analytics-native:compileReleaseJavaWithJavac FAILED

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.