• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

idehub/react-native-google-analytics-bridge: React Native bridge to the Google A ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

idehub/react-native-google-analytics-bridge

开源软件地址(OpenSource Url):

https://github.com/idehub/react-native-google-analytics-bridge

开源编程语言(OpenSource Language):

Objective-C 53.8%

开源软件介绍(OpenSource Introduction):

Deprecation notice

On November 13th 2018 Google issued the following statement:

We want to let you know that in October 2019 we will begin to sunset our Google Analytics for mobile apps reporting and the Google Analytics Services SDK.

Data collection and processing for such properties will stop on October 31, 2019.

The message is quite clear, and therefore I am officially deprecating this library. If you want to continue using Google's solutions for analytics, I recommend you move to Google Analytics for Firebase instead.

For React Native, there is a great library called react-native-firebase which implements Analytics (and other Firebase solutions).

I will continue to support this library for minor fixes, but no major changes will occur. The repository itself will be archived sometime in 2019.

Thanks to everyone who have used or contributed to this library!

- Christian (@cbrevik)

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");

Version 6 breaking changes!

If you are upgrading to version 6 from an older version, read this wiki post for important details.

The newest version of this library has a new API surface. The API changes are in most cases backwards-compatible.

Important: If you are using ecommerce or custom dimensions, you probably have to migrate to new API if you upgrade!

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);
  });

// You can also register Function Call tag handlers when the container is open.
GoogleTagManager.registerFunctionCallTagHandler(
  "some_function", // Must be equal to Function Name field when the tag was configured.
  (functionName, tagArguments) => {
    // functionName is passed for convenience. In this example it will be equal to "some_function".
    // tagArguments is an object and is populated based on Tag configuration in TagManager interface.
    console.log("Handling Function Call tag:", functionName);
  }
)

JavaScript API

Table of Contents

GoogleAnalyticsSettings

Settings which are applied across all trackers.

setOptOut

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.

Parameters
Examples
GoogleAnalyticsSettings.setOptOut(true);

setDispatchInterval

Sets the trackers dispatch interval. 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.

Parameters
Examples
GoogleAnalyticsSettings.setDispatchInterval(30);

setDryRun

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.

Parameters
Examples
GoogleAnalyticsSettings.setDryRun(true);

GoogleAnalyticsTracker

Examples

// Constructing a tracker is simple:
import { GoogleAnalyticsTracker } from "react-native-google-analytics-bridge";
const tracker = new GoogleAnalyticsTracker("UA-12345-1");
tracker.trackScreenView("Home");

// You can have multiple trackers if you have several tracking ids
const tracker2 = new GoogleAnalyticsTracker("UA-12345-2");
// One optional feature as well is constructing a tracker with a CustomDimensionsFieldIndexMap, to map custom dimension field names to index keys:
const fieldIndexMap = { customerType: 1 };
const tracker3 = new GoogleAnalyticsTracker("UA-12345-3", fieldIndexMap);

// This is because the Google Analytics API expects custom dimensions to be tracked by index keys, and not field names.
// Here the underlying logic will transform the custom dimension, so what ends up being sent to GA is { 1: 'Premium' }:
tracker3.trackScreenView("Home", { customDimensions: { customerType: "Premium" } });

// If you do not use a CustomDimensionsFieldIndexMap, you will have to use index as keys instead for custom dimensions:
tracker.trackScreenView("Home", { customDimensions: { 1: "Premium" } });

trackScreenView

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.

Parameters
  • screenName string (Required) The name of the current screen
  • payload HitPayload (Optional) An object containing the hit payload (optional, default null)
Examples
tracker.trackScreenView('Home');
// With payload:
const payload = { impressionList: "Sale", impressionProducts: [ { id: "PW928", name: "Premium bundle" } ] };
tracker.trackScreenView("SplashModal", payload);

trackEvent

Track an event that has occured

Parameters
  • category string (Required) The event category
  • action string (Required) The event action
  • eventMetadata EventMetadata (Optional) An object containing event metadata
  • payload HitPayload (Optional) An object containing the hit payload (optional, default null)
Examples
tracker.trackEvent("DetailsButton", "Click");
// Track event with label and value
tracker.trackEvent("AppVersionButton", "Click", { label: "v1.0.3", value: 22 });
// Track with a payload (ecommerce in this case):
const product = {
  id: "P12345",
  name: "Android Warhol T-Shirt",
  category: "Apparel/T-Shirts",
  brand: "Google",
  variant: "Black",
  price: 29.2,
  quantity: 1,
  couponCode: "APPARELSALE"
};
const transaction = {
  id: "T12345",
  affiliation: "Google Store - Online",
  revenue: 37.39,
  tax: 2.85,
  shipping: 5.34,
  couponCode: "SUMMER2013"
};
const productAction = {
  transaction,
  action: 7 // Purchase action, see ProductActionEnum
}
const payload = { products: [ product ], productAction: productAction }
tracker.trackEvent("FinalizeOrderButton", "Click", null, payload);

trackTiming

Track a timing measurement

Parameters
  • category string (Required) The event category
  • interval number (Required) The timing measurement in milliseconds
  • timingMetadata TimingMetadata (Required) An object containing timing metadata
  • payload HitPayload (Optional) An object containing the hit payload (optional, default null)
Examples
tracker.trackTiming("testcategory", 2000, { name: "LoadList" }); // name metadata is required
// With optional label:
tracker.trackTiming("testcategory", 2000, { name: "LoadList", label: "v1.0.3" });

trackException

Track an exception

Parameters
  • error string (Required) The description of the error
  • fatal boolean (Optional) A value indiciating if the error was fatal, defaults to false (optional, default false)
  • payload HitPayload (Optional) An object containing the hit payload (optional, default null)
Examples
try {
  ...
} catch(error) {
  tracker.trackException(error.message, false);
}

trackSocialInteraction

Track a social interaction, Facebook, Twitter, etc.

Parameters
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap