Skip to content

Commit

Permalink
Merge pull request aksonov#21 from idehub/js-trackerId
Browse files Browse the repository at this point in the history
Possibility to configure tracking code in JS
  • Loading branch information
cbrevik committed Mar 20, 2016
2 parents 4287d9f + 94daa04 commit 3a5c2e6
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 81 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The key difference with the native bridge is that you get a lot of the metadata
You will only have to send in a few parameteres when tracking, e.g:
```javascript
const GoogleAnalytics = require('react-native-google-analytics-bridge');
GoogleAnalytics.setTrackerId('UA-12345-1');

GoogleAnalytics.trackScreenView('Home');
GoogleAnalytics.trackEvent('testcategory', 'testaction');
Expand All @@ -17,9 +18,7 @@ GoogleAnalytics.trackEvent('testcategory', 'testaction');
1. `npm install --save react-native-google-analytics-bridge`
2. `rnpm link react-native-google-analytics-bridge`

With this, [rnpm](https://github.com/rnpm/rnpm) will do most of the heavy lifting for linking, **but** you will still need to do some of the manual steps below.

These are step 5 and 6 from the iOS installation, and step 4 from the Android installation. Specifically for Android step 4, you'll have to add the tracking id.
With this, [rnpm](https://github.com/rnpm/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.

## Manual installation iOS

Expand All @@ -32,11 +31,7 @@ These are step 5 and 6 from the iOS installation, and step 4 from the Android in
2. SystemConfiguration.framework
3. libz.tbd
4. libsqlite3.0.tbd
6. Under your project properties ➜ "Info", add a new line with the following:
1. Key: GAITrackingId
2. Type: String
3. Value: UA-12345-1 (in other words, your own tracking id).
7. **Optional step**: If you plan on using the advertising identifier (IDFA), then you need to do two things:
6. **Optional step**: If you plan on using the advertising identifier (IDFA), then you need to do two things:
1. Add AdSupport.framework under "Link Binary With Libraries". (As with the other frameworks in step 5).
2. Go to Xcode ➜ `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.

Expand Down Expand Up @@ -87,8 +82,8 @@ Consult [this guide](https://developer.android.com/sdk/installing/adding-package
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
// Step 2; register package, with your GA tracking id:
.addPackage(new GoogleAnalyticsBridgePackage("UA-12345-1"))
// Step 2; register package:
.addPackage(new GoogleAnalyticsBridgePackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
Expand All @@ -100,6 +95,16 @@ Consult [this guide](https://developer.android.com/sdk/installing/adding-package

## Javascript API

### setTrackerId(trackerId)
* **trackerId (required):** String, your tracker id, something like: UA-12345-1

**Important**: Call **once** on app startup to set the tracker id for all subsequent static calls.

```javascript
const GoogleAnalytics = require('react-native-google-analytics-bridge');
GoogleAnalytics.setTrackerId('UA-12345-1')
```

### trackScreenView(screenName)

* **screenName (required):** String, name of current screen
Expand Down Expand Up @@ -228,7 +233,9 @@ GoogleAnalytics.setUser('12345678');

* **enabled (required):** Boolean, true to allow IDFA collection, defaults to `true`.

**Important**: For iOS you can only use this method if you have done the optional step 7 from the installation guide.
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](https://developers.google.com/analytics/devguides/collection/ios/v3/campaigns#ios-install) for more info.

Expand Down Expand Up @@ -305,6 +312,7 @@ In order to control the `logLevel` you can add an item in your `Info.plist` with

## Roadmap

- [ ] Support for A/B testing
- [ ] Ecommerce: checkout process
- [ ] Ecommerce: impressions
- [ ] Campaigns
Expand All @@ -315,4 +323,4 @@ In order to control the `logLevel` you can add an item in your `Info.plist` with
## peerDependencies
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.
I've decided to remove the React Native peerDependency since some users have had issues with how npm handles peerDependencies, especially with -rc versions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public GoogleAnalyticsBridge(ReactApplicationContext reactContext, String tracki
_trackingId = trackingId;
}

private String _trackingId;
private final String _trackingId;

@Override
public String getName() {
Expand All @@ -48,12 +48,13 @@ synchronized GoogleAnalytics getAnalyticsInstance() {
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("nativeTrackerId", _trackingId);
return constants;
}

@ReactMethod
public void trackScreenView(String screenName){
Tracker tracker = getTracker(_trackingId);
public void trackScreenView(String trackerId, String screenName){
Tracker tracker = getTracker(trackerId);

if (tracker != null)
{
Expand All @@ -64,8 +65,8 @@ public void trackScreenView(String screenName){
}

@ReactMethod
public void trackEvent(String category, String action, ReadableMap optionalValues){
Tracker tracker = getTracker(_trackingId);
public void trackEvent(String trackerId, String category, String action, ReadableMap optionalValues){
Tracker tracker = getTracker(trackerId);

if (tracker != null)
{
Expand All @@ -87,8 +88,8 @@ public void trackEvent(String category, String action, ReadableMap optionalValue
}

@ReactMethod
public void trackTiming(String category, Double value, ReadableMap optionalValues){
Tracker tracker = getTracker(_trackingId);
public void trackTiming(String trackerId, String category, Double value, ReadableMap optionalValues){
Tracker tracker = getTracker(trackerId);

if (tracker != null)
{
Expand All @@ -110,8 +111,8 @@ public void trackTiming(String category, Double value, ReadableMap optionalValue
}

@ReactMethod
public void trackPurchaseEvent(ReadableMap product, ReadableMap transaction, String eventCategory, String eventAction){
Tracker tracker = getTracker(_trackingId);
public void trackPurchaseEvent(String trackerId, ReadableMap product, ReadableMap transaction, String eventCategory, String eventAction){
Tracker tracker = getTracker(trackerId);

if (tracker != null) {
Product ecommerceProduct = new Product()
Expand Down Expand Up @@ -143,9 +144,9 @@ public void trackPurchaseEvent(ReadableMap product, ReadableMap transaction, Str
}

@ReactMethod
public void trackException(String error, Boolean fatal)
public void trackException(String trackerId, String error, Boolean fatal)
{
Tracker tracker = getTracker(_trackingId);
Tracker tracker = getTracker(trackerId);

if (tracker != null) {
tracker.send(new HitBuilders.ExceptionBuilder()
Expand All @@ -156,29 +157,29 @@ public void trackException(String error, Boolean fatal)
}

@ReactMethod
public void setUser(String userId)
public void setUser(String trackerId, String userId)
{
Tracker tracker = getTracker(_trackingId);
Tracker tracker = getTracker(trackerId);

if (tracker != null) {
tracker.set("&uid", userId);
}
}

@ReactMethod
public void allowIDFA(Boolean enabled)
public void allowIDFA(String trackerId, Boolean enabled)
{
Tracker tracker = getTracker(_trackingId);
Tracker tracker = getTracker(trackerId);

if (tracker != null) {
tracker.enableAdvertisingIdCollection(enabled);
}
}

@ReactMethod
public void trackSocialInteraction(String network, String action, String targetUrl)
public void trackSocialInteraction(String trackerId, String network, String action, String targetUrl)
{
Tracker tracker = getTracker(_trackingId);
Tracker tracker = getTracker(trackerId);

if (tracker != null) {
tracker.send(new HitBuilders.SocialBuilder()
Expand Down Expand Up @@ -210,8 +211,8 @@ public void setDispatchInterval(Integer intervalInSeconds){
}

@ReactMethod
public void setTrackUncaughtExceptions(Boolean enabled){
Tracker tracker = getTracker(_trackingId);
public void setTrackUncaughtExceptions(String trackerId, Boolean enabled){
Tracker tracker = getTracker(trackerId);

if (tracker != null)
{
Expand All @@ -221,8 +222,8 @@ public void setTrackUncaughtExceptions(Boolean enabled){


@ReactMethod
public void setAnonymizeIp(Boolean enabled){
Tracker tracker = getTracker(_trackingId);
public void setAnonymizeIp(String trackerId, Boolean enabled){
Tracker tracker = getTracker(trackerId);

if (tracker != null)
{
Expand All @@ -232,11 +233,11 @@ public void setAnonymizeIp(Boolean enabled){

@ReactMethod
public void setOptOut(Boolean enabled){
GoogleAnalytics analytics = getAnalyticsInstance();
GoogleAnalytics analytics = getAnalyticsInstance();

if (analytics != null)
{
if (analytics != null)
{
analytics.setAppOptOut(enabled);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public GoogleAnalyticsBridgePackage(String trackingId) {
_trackingId = trackingId;
}

public GoogleAnalyticsBridgePackage() {
this(null);
}

private String _trackingId;

@Override
Expand Down
8 changes: 8 additions & 0 deletions example/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const GoogleAnalytics = require('react-native-google-analytics-bridge');

var example = React.createClass({
render: function() {
GoogleAnalytics.setTrackerId('UA-12345-2');

GoogleAnalytics.setDryRun(true);
GoogleAnalytics.trackEvent('testcategory', 'Hello Android');
GoogleAnalytics.trackScreenView('Home');
Expand Down Expand Up @@ -52,6 +54,12 @@ var example = React.createClass({
GoogleAnalytics.setUser('12345678');

GoogleAnalytics.allowIDFA(true);

GoogleAnalytics.setDispatchInterval(30);

GoogleAnalytics.setOptOut(true);

GoogleAnalytics.setAnonymizeIp(true);

return (
<View style={styles.container}>
Expand Down
8 changes: 8 additions & 0 deletions example/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const GoogleAnalytics = require('react-native-google-analytics-bridge');

var example = React.createClass({
render: function() {
GoogleAnalytics.setTrackerId('UA-12345-2');

GoogleAnalytics.setDryRun(true);
GoogleAnalytics.trackEvent('testcategory', 'Hello iOS');
GoogleAnalytics.trackScreenView('Home');
Expand Down Expand Up @@ -52,6 +54,12 @@ var example = React.createClass({
GoogleAnalytics.setUser('12345678');

GoogleAnalytics.allowIDFA(true);

GoogleAnalytics.setDispatchInterval(30);

GoogleAnalytics.setOptOut(true);

GoogleAnalytics.setAnonymizeIp(true);

return (
<View style={styles.container}>
Expand Down
Loading

0 comments on commit 3a5c2e6

Please sign in to comment.