
Security News
Rspack Introduces Rslint, a TypeScript-First Linter Written in Go
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
react-native-google-analytics-bridge
Advanced tools
React Native bridge for using native Google Analytics libraries on iOS and Android
Google Analytics Bridge is built to provide an easy interface to the native Google Analytics libraries on both iOS and Android.
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:
const GoogleAnalytics = require('react-native-google-analytics-bridge');
GoogleAnalytics.setTrackerId('UA-12345-1');
GoogleAnalytics.trackScreenView('Home');
GoogleAnalytics.trackEvent('testcategory', 'testaction');
npm install --save react-native-google-analytics-bridge
rnpm link react-native-google-analytics-bridge
With this, rnpm will do most of the heavy lifting for linking, but for iOS you will still need to do step 5 from the manual installation guide below.
npm install --save react-native-google-analytics-bridge
Add Files to <your project>
.node_modules
➜ react-native-google-analytics-bridge
➜ ios
➜ RCTGoogleAnalyticsBridge
and add the RCTGoogleAnalyticsBridge.xcodeproj
file.Libraries
➜ RCTGoogleAnalyticsBridge.xcodeproj
➜ right-click google-analytics-lib
. Here you need to Add files to ..
, and add libAdIdAccess.a
from the google-analytics-lib
directory. This directory is located in the same node_modules
path as in step 3.Make sure you have the following SDK packages installed in the Android SDK Manager:
Consult this guide if you are unsure how to do this. Specifically step 3 for the mentioned packages.
npm install --save react-native-google-analytics-bridge
android/setting.gradle
...
include ':react-native-google-analytics-bridge', ':app'
project(':react-native-google-analytics-bridge').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-google-analytics-bridge/android')
android/app/build.gradle
...
dependencies {
...
compile project(':react-native-google-analytics-bridge')
}
MainActivity.java
// Step 1; import package:
import com.idehub.GoogleAnalyticsBridge.GoogleAnalyticsBridgePackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
// Step 2; register package:
.addPackage(new GoogleAnalyticsBridgePackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
...
}
...
Important: Call once on app startup to set the tracker id for all subsequent static calls.
const GoogleAnalytics = require('react-native-google-analytics-bridge');
GoogleAnalytics.setTrackerId('UA-12345-1')
Important: 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.
See the Google Analytics docs for more info
GoogleAnalytics.trackScreenView('Home')
See the Google Analytics docs for more info.
GoogleAnalytics.trackEvent('testcategory', 'testaction');
// or
GoogleAnalytics.trackEvent('testcategory', 'testaction', {label: 'v1.0.3', value: 22});
See the Google Analytics docs for more info.
GoogleAnalytics.trackTiming('testcategory', 13000);
// or
GoogleAnalytics.trackTiming('testcategory', 13000, {name: 'loadList', label: 'v1.0.3'});
See the Google Analytics docs for more info.
GoogleAnalytics.trackPurchaseEvent({
id: 'P12345',
name: 'Android Warhol T-Shirt',
category: 'Apparel/T-Shirts',
brand: 'Google',
variant: 'Black',
price: 29.20,
quantity: 1,
couponCode: 'APPARELSALE'
}, {
id: 'T12345',
affiliation: 'Google Store - Online',
revenue: 37.39,
tax: 2.85,
shipping: 5.34,
couponCode: 'SUMMER2013'
}, 'Ecommerce', 'Purchase');
See the Google Analytics docs for more info.
try {
...
} catch(error) {
GoogleAnalytics.trackException(error.message, false);
}
See the Google Analytics docs for more info.
GoogleAnalytics.trackSocialInteraction('Twitter', 'Post');
See the Google Analytics for more info.
GoogleAnalytics.setUser('12345678');
true
.Also called advertising identifier collection, and is used for advertising features.
Important: For iOS you can only use this method if you have done the optional step 6 from the installation guide. Only enable this (and link the appropriate libraries) if you plan to use advertising features in your app, or else your app may get rejected from the AppStore.
See the Google Analytics for more info.
GoogleAnalytics.allowIDFA(true);
dryRun
flag should be enabled or not.When enabled the native library prevents any data from being sent to Google Analytics. This allows you to test or debug the implementation, without your test data appearing in your Google Analytics reports.
GoogleAnalytics.setDryRun(true);
Events, screen views, etc, are sent in batches to your tracker. This function allows you to configure how often (in seconds) the batches are sent to your tracker. Recommended to keep this around 20-120 seconds to preserve battery and network traffic. This is set to 20 seconds by default.
GoogleAnalytics.setDispatchInterval(30);
Sets if uncaught exceptions should be tracked. This is enabled by default.
GoogleAnalytics.setTrackUncaughtExceptions(true);
Sets if AnonymizeIp is enabled. This is disabled by default. If enabled the last octet of the IP address will be removed.
GoogleAnalytics.setAnonymizeIp(true);
Sets if OptOut is active and disables Google Analytics. This is disabled by default. Note: This has to be set each time the App starts.
GoogleAnalytics.setOptOut(true);
There is a divergence in how the iOS and Android versions of the native library handles logging.
For Android you can check the GA logs with your favorite terminal by using adb logcat
.
For iOS there is a logger in the internal library that writes events to the XCode output window.
In order to control the logLevel
you can add an item in your Info.plist
with the key GAILogLevel
. The value you use is a number which corresponds to your desired log-level:
This library should work with at least React Native 0.11 and up, but has been tested mostly with 0.17.
I've decided to remove the React Native peerDependency since some users have had issues with how npm handles peerDependencies, especially with -rc versions.
FAQs
React Native bridge for using native Google Analytics libraries on iOS and Android
We found that react-native-google-analytics-bridge demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.