New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-native-google-analytics-bridge

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-google-analytics-bridge

React Native bridge for using native Google Analytics libraries on iOS and Android

  • 6.0.0-rc10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
152
decreased by-53.52%
Maintainers
1
Weekly downloads
 
Created
Source

GoogleAnalyticsBridge npm version Build Status

Google Analytics Bridge is built to provide an easy interface to the native Google Analytics libraries on both iOS and Android.

Why a native bridge? Why not use just JavaScript?

The key difference with the native bridge is that you get a lot of the metadata handled automatically by the Google Analytics native library. This will include the device UUID, device model, viewport size, OS version etc.

You will only have to send in a few parameteres when tracking, e.g:

import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
let tracker = new GoogleAnalyticsTracker('UA-12345-1');

tracker.trackScreenView('Home');
tracker.trackEvent('testcategory', 'testaction');

Content

Installation and linking libraries

  • For React Native >= 0.40 use version 5.0.0 (and up) of this module.
  • For React Native < 0.40 use version 4.0.3.

Install with npm: npm install --save react-native-google-analytics-bridge

Or, install with yarn: yarn add react-native-google-analytics-bridge

Either way, then link with: react-native link react-native-google-analytics-bridge

If it doesn't work immediately after this, consult the manual installation guide. Both Android and iOS has a couple of prerequisite SDKs linked and installed.

Important: Does this library work with Expo? We have to sort of invert the question a bit, because it should be: does Expo work with other libraries? And the answer is no:

The most limiting thing about Expo is that you can’t add in your own native modules without detaching and using ExpoKit.

This includes using create-react-native-app which also makes use of Expo.

Usage

// You have access to three classes in this module:
import {
  GoogleAnalyticsTracker,
  GoogleTagManager,
  GoogleAnalyticsSettings
} from 'react-native-google-analytics-bridge';

// The tracker must be constructed, and you can have multiple:
let tracker1 = new GoogleAnalyticsTracker('UA-12345-1');
let tracker2 = new GoogleAnalyticsTracker('UA-12345-2');

tracker1.trackScreenView('Home');
tracker1.trackEvent('Customer', 'New');

// The GoogleAnalyticsSettings is static, and settings are applied across all trackers:
GoogleAnalyticsSettings.setDispatchInterval(30);
// Setting `dryRun` to `true` lets you test tracking without sending data to GA 
GoogleAnalyticsSettings.setDryRun(true);

// GoogleTagManager is also static, and works only with one container. All functions here are Promises:
GoogleTagManager.openContainerWithId("GT-NZT48")
  .then(() => {
    return GoogleTagManager.stringForKey("pack");
  })
  .then((pack) => {
    console.log("Pack: ", pack);
  })
  .catch((err) => {
    console.log(err);
  });

JavaScript API

There are three classes which can be imported from the react-native-google-analytics-bridge module.

Classes

CustomDimensionsFieldIndexMap
CustomDimensionsByIndex
CustomDimensionsByField
CustomMetrics
GoogleAnalyticsSettings
GoogleAnalyticsTracker
GoogleTagManager
HitPayload
Hit
ScreenHitHit
EventHitHit
TimingHitHit
ExceptionHitHit
SocialHitHit
NewGoogleAnalyticsTracker

CustomDimensionsFieldIndexMap

Kind: global class
Export:

CustomDimensionsByIndex

Kind: global class
Export:

CustomDimensionsByField

Kind: global class
Export:

CustomMetrics

Kind: global class
Export:

GoogleAnalyticsSettings

Kind: global class

GoogleAnalyticsSettings.setOptOut(enabled)

Sets if OptOut is active and disables Google Analytics This has to be set each time the App starts

Kind: static method of GoogleAnalyticsSettings

ParamType
enabledboolean

GoogleAnalyticsSettings.setDispatchInterval(intervalInSeconds)

Sets the trackers dispatch interval This will influence how often batches of events, screen views, etc are sent to your tracker.

Kind: static method of GoogleAnalyticsSettings

ParamType
intervalInSecondsnumber

GoogleAnalyticsSettings.setDryRun(enabled)

Sets if the tracker should have dry run enabled. If dry run is enabled, no analytics data will be sent to your tracker.

Kind: static method of GoogleAnalyticsSettings

ParamType
enabledboolean

GoogleAnalyticsTracker

Kind: global class

new GoogleAnalyticsTracker(trackerId, customDimensionsFieldsIndexMap)

Save all tracker related data that is needed to call native methods with proper data.

ParamTypeDescription
trackerIdstringYour tracker id, something like: UA-12345-1
customDimensionsFieldsIndexMapObjectCustom dimensions field/index pairs

Example

import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
let tracker = new GoogleAnalyticsTracker('UA-12345-1');

tracker.trackScreenView(screenName, payload)

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDefaultDescription
screenNamestring(Required) The name of the current screen
payloadObject(Optional) An object containing the hit payload

Example

tracker.trackScreenView('Home')

Track the current screen/view. Calling this will also set the "current view" for other calls. So events tracked will be tagged as having occured on the current view, Home in this example. This means it is important to track navigation, especially if events can fire on different views.

tracker.trackEvent(category, action, payload, label, value)

Track an event that has occured

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDefaultDescription
categorystring(Required) The event category
actionstring(Required) The event action
payloadObject(Optional) An object containing the hit payload
labelstringnull(Optional) An optional event label
valuenumber(Optional) An optional event value

tracker.trackTiming(category, interval, payload, name, label)

Track an event that has occured

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDefaultDescription
categorystring(Required) The event category
intervalnumber(Required) The timing measurement in milliseconds
payloadObject(Optional) An object containing the hit payload
namestringnull(Required) The timing name
labelstringnull(Optional) An optional timing label

tracker.trackException(error, fatal, payload)

Track an exception

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDefaultDescription
errorstring(Required) The description of the error
fatalbooleanfalse(Optional) A value indiciating if the error was fatal, defaults to false
payloadObject(Optional) An object containing the hit payload

tracker.trackSocialInteraction(network, action, targetUrl, payload)

Track a social interaction, Facebook, Twitter, etc.

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
networkstring
actionstring
targetUrlstring
payloadObject(Optional) An object containing the hit payload

tracker.setUser(userId)

Sets the current userId for tracking.

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
userIdstringThe current userId

tracker.setClient(clientId)

Sets the current clientId for tracking.

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
clientIdstringThe current userId

tracker.allowIDFA(enabled)

Sets if IDFA (identifier for advertisers) collection should be enabled

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDefaultDescription
enabledbooleantrue(Optional) Defaults to true

tracker.setAppName(appName)

Sets the trackers appName The Bundle name is used by default

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
appNamestring(Required)

tracker.setAppVersion(appVersion)

Sets the trackers appVersion

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
appVersionstring(Required)

tracker.setAnonymizeIp(enabled)

Sets if AnonymizeIp is enabled If enabled the last octet of the IP address will be removed

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
enabledboolean(Required)

tracker.setSamplingRate(sampleRatio)

Sets tracker sampling rate.

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
sampleRationumber(Required) Percentage 0 - 100

tracker.setCurrency(currencyCode)

Sets the currency for tracking.

Kind: instance method of GoogleAnalyticsTracker

ParamTypeDescription
currencyCodestring(Required) The currency ISO 4217 code

tracker.setTrackUncaughtExceptions(enabled)

Sets if uncaught exceptions should be tracked Important to note: On iOS this option is set on all trackers. On Android it is set per tracker. If you are using multiple trackers on iOS, this will enable & disable on all trackers.

Kind: instance method of GoogleAnalyticsTracker

ParamType
enabledboolean

GoogleTagManager

Kind: global class

GoogleTagManager.openContainerWithId(containerId) ⇒ Promise.<boolean>

Call once to open the container for all subsequent static calls.

Kind: static method of GoogleTagManager

ParamType
containerIdstring

GoogleTagManager.boolForKey(key) ⇒ Promise.<boolean>

Retrieves a boolean value with the given key from the opened container.

Kind: static method of GoogleTagManager

ParamType
keystring

GoogleTagManager.stringForKey(key) ⇒ Promise.<string>

Retrieves a string with the given key from the opened container.

Kind: static method of GoogleTagManager

ParamType
keystring

GoogleTagManager.doubleForKey(key) ⇒ Promise.<number>

Retrieves a number with the given key from the opened container.

Kind: static method of GoogleTagManager

ParamType
keystring

GoogleTagManager.pushDataLayerEvent(event) ⇒ Promise.<boolean>

Push a datalayer event for Google Analytics through Google Tag Manager. The event must have at least one key "event" with event name. You can add optional values on top of that, example: {event: "eventName", pageId: "/home"}

Kind: static method of GoogleTagManager

ParamTypeDescription
eventObjectAn Map<String, Object> containing key and value pairs. It must have at least one key "event" with event name

HitPayload

Kind: global class
Properties

NameTypeDescription
productsArray.<Product>(Optional) Products which an action has occured on.
productActionProductAction(Optional) The action which occured on the products. Required if products is not empty.
impressionProductsArray.<Product>(Optional) Products which has had an impression.
impressionListstring(Optional) The list which product impressions occured on. Required if impressionProducts is not empty.
impressionSourcestring(Optional) The source which impression occured from (Android only).
customDimensionsCustomDimensionsByIndex | CustomDimensionsByField(Optional) Custom dimensions.
customMetricsCustomMetrics(Optional) Custom metrics.
utmCampaignUrlstring(Optional) UTM campaign URL.
startSessionnumber(Optional) Start new session.

new HitPayload()

All properties on the payload is optional. Preferably leave them empty unless you actually need to send something extra.

payload.addProduct(product) ⇒ HitPayload

Adds a product in the payload. productAction must also be set on the payload if you add products.

Kind: instance method of HitPayload

ParamType
productProduct

payload.addProductImpression(product) ⇒ HitPayload

Adds a product impression. impressionList must also be set on the payload if you add impression products.

Kind: instance method of HitPayload

ParamType
productProduct

Hit

Kind: global class
Properties

NameTypeDefaultDescription
payloadHitPayload(Optional) Payload to send with the hit.

new Hit()

Base class for all Hits.

ScreenHit ⇐ Hit

Kind: global class
Extends: Hit
Export:
Properties

NameTypeDescription
screenNamestringThe current screen name.

ScreenHit.ScreenHit

Kind: static class of ScreenHit

new ScreenHit(screenName)

Creates an instance of ScreenHit.

ParamType
screenNamestring

EventHit ⇐ Hit

Kind: global class
Extends: Hit
Export:
Properties

NameTypeDefaultDescription
categorystringThe event category
actionstringThe event action
labelstringnullThe event label
valuenumberThe event value

EventHit.EventHit

Kind: static class of EventHit

new EventHit(category, action, [label], [value])

Creates an instance of EventHit.

ParamTypeDefault
categorystring
actionstring
[label]stringnull
[value]number

TimingHit ⇐ Hit

Kind: global class
Extends: Hit
Export:
Properties

NameTypeDefaultDescription
categorystringThe timing category
intervalnumberThe timing interval
namestringThe timing name
labelstringnullThe timing label

TimingHit.TimingHit

Kind: static class of TimingHit

new TimingHit(category, interval, name, [label])

Creates an instance of TimingHit.

ParamTypeDefault
categorystring
intervalnumber
namestring
[label]stringnull

ExceptionHit ⇐ Hit

Kind: global class
Extends: Hit
Export:
Properties

NameTypeDefaultDescription
errorstringThe error
fatalbooleanfalseIf the error was fatal. Defaults to false.

ExceptionHit.ExceptionHit

Kind: static class of ExceptionHit

new ExceptionHit(error, [fatal])

Creates an instance of ExceptionHit.

ParamTypeDefault
errorstring
[fatal]booleanfalse

SocialHit ⇐ Hit

Kind: global class
Extends: Hit
Export:
Properties

NameTypeDescription
networkstringThe social network
actionstringThe social action. +1, like, etc
targetUrlstringThe target url.

SocialHit.SocialHit

Kind: static class of SocialHit

new SocialHit(network, action, targetUrl)

Creates an instance of SocialHit.

ParamType
networkstring
actionstring
targetUrlstring

NewGoogleAnalyticsTracker

Kind: global class

new NewGoogleAnalyticsTracker(trackerId, customDimensionsFieldsIndexMap)

Save all tracker related data that is needed to call native methods with proper data.

ParamTypeDescription
trackerIdstringYour tracker id, something like: UA-12345-1
customDimensionsFieldsIndexMapObjectCustom dimensions field/index pairs

Example

import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
let tracker = new GoogleAnalyticsTracker('UA-12345-1');

tracker.trackScreen(screenName)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
screenNamestring

tracker.trackScreenHit(hit)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
hitScreenHit

tracker.trackEvent(category, action, [label], [value])

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDefault
categorystring
actionstring
[label]stringnull
[value]number

tracker.trackEvenHit(hit)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
hitEventHit

tracker.trackTiming(category, interval, name, [label])

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDefault
categorystring
intervalnumber
namestring
[label]stringnull

tracker.trackTimingHit(hit)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
hitTimingHit

tracker.trackException(error, [fatal])

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDefault
errorstring
[fatal]booleanfalse

tracker.trackExceptionHit(hit)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
hitExceptionHit

tracker.trackSocial(network, action, targetUrl)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
networkstring
actionstring
targetUrlstring

tracker.trackSocialHit(hit)

Kind: instance method of NewGoogleAnalyticsTracker

ParamType
hitSocialHit

tracker.setUser(userId)

Sets the current userId for tracking.

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
userIdstringThe current userId

tracker.setClient(clientId)

Sets the current clientId for tracking.

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
clientIdstringThe current userId

tracker.allowIDFA(enabled)

Sets if IDFA (identifier for advertisers) collection should be enabled

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDefaultDescription
enabledbooleantrue(Optional) Defaults to true

tracker.setAppName(appName)

Sets the trackers appName The Bundle name is used by default

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
appNamestring(Required) The app name

tracker.setAppVersion(appVersion)

Sets the trackers appVersion

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
appVersionstring(Required) The app version

tracker.setAnonymizeIp(enabled)

Sets if AnonymizeIp is enabled If enabled the last octet of the IP address will be removed

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
enabledboolean(Required) Enable anonymize IP

tracker.setSamplingRate(sampleRatio)

Sets tracker sampling rate.

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
sampleRationumber(Required) Percentage 0 - 100

tracker.setCurrency(currencyCode)

Sets the currency for tracking.

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
currencyCodestring(Required) The currency ISO 4217 code

tracker.setTrackUncaughtExceptions(enabled)

Sets if uncaught exceptions should be tracked Important to note: On iOS this option is set on all trackers. On Android it is set per tracker. If you are using multiple trackers on iOS, this will enable & disable on all trackers.

Kind: instance method of NewGoogleAnalyticsTracker

ParamTypeDescription
enabledbooleanShould enable uncaught exception tracking

Keywords

FAQs

Package last updated on 20 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc