Giter Site home page Giter Site logo

chansuke / react-native-td Goto Github PK

View Code? Open in Web Editor NEW

This project forked from quipper/react-native-td

0.0 3.0 0.0 186 KB

An unofficial React Native SDK for Treasure Data.

License: Apache License 2.0

Ruby 4.40% Java 36.21% Python 4.71% JavaScript 1.01% Objective-C 30.09% TypeScript 23.58%

react-native-td's Introduction

react-native-td CircleCI npm version

react-native-td is an unofficial React Native SDK for Treasure Data.

The module is a light-weight layer sitting on-top of the TreasureData SDK for both iOS and Android.

Installation

You can install react-native-td into your project in the following way.

npm install --save react-native-td
react-native link react-native-td

iOS configuration

If you haven't introduced cocoapods, install it and run pod init under ios/and make sure lines below are in Podfile.

+ pod 'RNTreasureData', :path => '../node_modules/react-native-td'
+ pod 'React', path: '../node_modules/react-native'
+ pod 'yoga', path: '../node_modules/react-native/ReactCommon/yoga/Yoga.podspec'

Usage

Basically react-native-td imitates the interface of TreasureData iOS/Android SDK as close as possible.

Hence to know more detail check official documents below.

Instantiate with your API key

import TreasureData from "react-native-td";

TreasureData.initialize("your API key");

We recommend to use a write-only API key for the SDK. To obtain one, please:

  1. Login to the Treasure Data Console at http://console.treasuredata.com;
  2. Visit your Profile page at http://console.treasuredata.com/users/current;
  3. Insert your password under the 'API Keys' panel;
  4. In the bottom part of the panel, under 'Write-Only API keys', either copy the API key or click on 'Generate New' and copy the new API key.

Add a event to local buffer

To add an event to local buffer, you can call addEvent or addEventWithCallback API.

TreasureData.addEventWithCallback(
  { event: "event_name" },
  "database_name",
  "record_name",
  () => {
    console.log("onSuccess is called.");
  },
  (error, message) => {
    console.log(`Error: ${error}`);
    console.log(`Message: ${message}`);
  }
);

Or simply:

TreasureData.addEvent("database_name", "table_name", { event: "event_name" });

Upload buffered events to Treasure Data

To upload events buffered events to Treasure Data, you can call uploadEvents or uploadEventsWithCallback API.

TreasureData.uploadEventsWithCallback(
  () => {
    console.log("onSuccess is called.");
  },
  (error, message) => {
    console.log(`Error: ${error}`);
    console.log(`Message: ${message}`);
  }
);

Or simply:

TreasureData.uploadEvents();

Start/End session

TreasureData.startSession("table_name");

TreasureData.endSession("table_name");

If you want to handle the following case, use a pair of methods startSession and endSession for global session tracking.

  • User opens the application and starts session tracking using startSession
  • User moves to home screen and finishes the session using endSession
  • User reopens the application and restarts session tracking within default 10 seconds. But you want to deal with this new session as the same session.
TreasureData.setSessionTimeoutMilli(30 * 1000); // Default is 10 seconds

TreasureData.startSession();

TreasureData.endSession();

In this case, you can get the current session ID using getSessionId.

TreasureData.getSessionId().then(sessionId => {
  console.log(sessionId);
});

Detect if it's the first running

You can detect if it's the first running or not easily using isFirstRun method and then clear the flag with clearFirstRun.

TreasureData.isFirstRun()
  .then(() => {
    TreasureData.clearFirstRun();
    TreasureData.uploadEvents();
  })
  .catch(error => {
    console.log(error);
  });

About Error code

addEventWithCallback and uploadEventsWithCallback methods call back onError with errorCode argument.

This argument is useful to know the cause type of the error. There are the following error codes.

  • init_error : The initialization failed.
  • invalid_param : The parameter passed to the API was invalid
  • invalid_event : The event was invalid
  • data_conversion : Failed to convert the data to/from JSON
  • storage_error : Failed to read/write data in the storage
  • network_error : Failed to communicate with the server due to network problem
  • server_response : The server returned an error response

Additional Configuration

Endpoint

TreasureData.initializeApiEndpoint("https://in.treasuredata.com");
TreasureData.initialize("your API key");

Encryption key

TreasureData.initializeEncryptionKey("hello world");

Default database

TreasureData.setDefaultDatabase("default_db");

Adding UUID of the device to each event automatically

TreasureData.enableAutoAppendUniqId();

Adding an UUID to each event record automatically

TreasureData.enableAutoAppendRecordUUID();

Adding device model information to each event automatically

TreasureData.enableAutoAppendModelInformation();

Adding application package version information to each event automatically

TreasureData.enableAutoAppendAppInformation();

Adding locale configuration information to each event automatically

TreasureData.enableAutoAppendLocaleInformation();

Use server side upload timestamp

TreasureData.enableServerSideUploadTimestamp();
// Add server side upload time as a customized column name
TreasureData.enableServerSideUploadTimestamp("server_upload_time");

Enable/Disable debug log

TreasureData.enableLogging();
TreasureData.disableLogging();

react-native-td's People

Contributors

hotchemi avatar

Watchers

Yusuke Abe avatar James Cloos avatar  avatar

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.