Skip to content

mtebele/react-native-tiktok-business-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-tiktok-business-sdk

npm version Build Status npm downloads License

A React Native bridge for the TikTok Business SDK

This library exposes native methods for initializing the TikTok SDK, identifying users, logging out, and tracking various events (standard, content, and custom events) via the TikTok Business SDK.

Installation

Install the package using npm (or yarn):

npm install react-native-tiktok-business-sdk

yarn add react-native-tiktok-business-sdk

Usage

Below are examples of how to use the various methods exposed by the library.

Importing the Library

The package exposes a main object, TikTokBusiness, that aggregates all the methods and enums. For example:

import { TikTokBusiness } from 'react-native-tiktok-business-sdk';

Initialize the SDK

Before using any event tracking methods, you must initialize the SDK. You can call initializeSdk with your appId and tiktokAppId, and optionally set debug mode. (The debug parameter defaults to false if not provided).

async function initializeTikTokSDK() {
  try {
    await TikTokBusiness.initializeSdk(
      'YOUR_APP_ID',
      'YOUR_TIKTOK_APP_ID',
      true
    );
    // SDK is now initialized, and tracking is active.
  } catch (error) {
    console.error('Error initializing TikTok SDK:', error);
  }
}

initializeTikTokSDK();

Identify a User

Call the identify method to report user information.

async function identifyUser() {
  try {
    await TikTokBusiness.identify(
      'externalId',
      'externalUserName',
      'phoneNumber',
      '[email protected]'
    );
  } catch (error) {
    console.error('Error identifying user:', error);
  }
}

Logout

Log out the user with the logout method:

async function logoutUser() {
  try {
    await TikTokBusiness.logout();
  } catch (error) {
    console.error('Error logging out:', error);
  }
}

Track a Standard Event

Use trackEvent to report standard events. You can optionally pass an event ID and additional properties. For example, to track a "REGISTRATION" event:

async function trackStandardEvent() {
  try {
    await TikTokBusiness.trackEvent(TikTokEventName.REGISTRATION);
  } catch (error) {
    console.error('Error tracking standard event:', error);
  }
}

Track a Content Event

For events that require content details (e.g., “ADD_TO_CART”, “CHECK_OUT”, etc.), use trackContentEvent. The method accepts an event type string and a parameters object. The CONTENTS property should be an array of objects with keys defined in the TikTokContentParameter enum.

TikTokBusiness.trackContentEvent(TikTokContentEventName.CHECK_OUT, {
  [TikTokContentEventParameter.CURRENCY]: 'USD',
  [TikTokContentEventParameter.VALUE]: '4.99',
  [TikTokContentEventParameter.CONTENT_TYPE]: 'Purchase',
  [TikTokContentEventParameter.CONTENTS]: [
    {
      [TikTokContentEventContentsParameter.CONTENT_ID]: 'ABC',
      [TikTokContentEventContentsParameter.CONTENT_NAME]: 'Coffee',
      [TikTokContentEventContentsParameter.PRICE]: '4.99',
      [TikTokContentEventContentsParameter.QUANTITY]: 1,
    },
  ],
});

Track a Custom Event

If you need to report an event that isn’t standard, use trackCustomEvent and pass the event name and a properties object.

async function trackCustomEvent() {
  try {
    await TikTokBusiness.trackCustomEvent('custom_event_name', {
      description: 'This is a custom event',
      value: 200,
      currency: 'USD',
    });
  } catch (error) {
    console.error('Error tracking custom event:', error);
  }
}

Enums

The library exports the following enums to ensure consistency when reporting events:

  • TikTokEventName – Standard event names.
  • TikTokContentEventName – Content event names.
  • TikTokContentEventParameter – Parameters for content events.
  • TikTokContentEventContentsParameter – Parameters for the contents parameter of content events.

Example:

import {
  TikTokBusiness,
  TikTokContentEventContentsParameter,
  TikTokEventName,
  TikTokContentEventParameter,
  TikTokContentEventName,
} from 'react-native-tiktok-business-sdk';

Proguard

If you're using Proguard to optimize your app, you must add rules to prevent Proguard from removing classes.

-keep class com.tiktok.** { *; }
-keep class com.android.billingclient.api.** { *; }

Upcoming Features

  • Add example app
  • Unit and integration tests
  • Add support for TurboModules
  • Improve TypeScript declarations and documentation
  • Enhance error handling and logging mechanisms
  • Support additional TikTok Business SDK events

Contributing

See the contributing guide to learn how to contribute to the repository and our development workflow.

License

MIT