-
Notifications
You must be signed in to change notification settings - Fork 13
/
CustomerioTracking.tsx
128 lines (115 loc) · 3.93 KB
/
CustomerioTracking.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { NativeModules, Platform } from 'react-native';
import {
CustomerioConfig,
CustomerIOEnv,
PackageConfig,
} from './CustomerioConfig';
import { Region } from './CustomerioEnum';
var pjson = require("customerio-reactnative/package.json");
const LINKING_ERROR =
`The package 'customerio-reactnative' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
/**
* Get CustomerioReactnative native module
*/
const CustomerioReactnative = NativeModules.CustomerioReactnative
? NativeModules.CustomerioReactnative
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
class CustomerIO {
/**
* To initialize the package using workspace credentials
* such as siteId, APIKey and region as optional.
*
* @param env use CustomerIOEnv class to set environment variables such as siteId, apiKey, region, org id
* @param config set config for the package eg trackApiUrl etc
* @returns
*/
static initialize(
env: CustomerIOEnv,
config: CustomerioConfig = new CustomerioConfig()
) {
let pversion = pjson.version ?? '';
let expoVersion = pjson.expoVersion ?? '';
const packageConfig = new PackageConfig();
packageConfig.source = 'ReactNative';
packageConfig.version = pversion;
if (expoVersion != '') {
packageConfig.source = 'Expo';
packageConfig.version = expoVersion;
}
if (env.organizationId && env.organizationId != '') {
console.warn('{organizationId} is deprecated and will be removed in future releases, please remove {organizationId} and enable in-app messaging using {CustomerioConfig.enableInApp}');
config.enableInApp = true;
console.warn('{config.enableInApp} set to {true} because {organizationId} was added');
}
return CustomerioReactnative.initialize(env, config, packageConfig);
}
/**
* Identify a person using a unique identifier, eg. email id.
* Note that you can identify only 1 profile at a time. In case, multiple
* identifiers are attempted to be identified, then the last identified profile
* will be removed automatically.
*
* @param identifier unique identifier for a profile
* @param body (Optional) data to identify a profile
*/
static identify(identifier: string, body: Object) {
CustomerioReactnative.identify(identifier, body);
}
/**
* Call this function to stop identifying a person.
*
* If a profile exists, clearIdentify will stop identifying the profile.
* If no profile exists, request to clearIdentify will be ignored.
*/
static clearIdentify() {
CustomerioReactnative.clearIdentify();
}
/**
* To track user events like loggedIn, addedItemToCart etc.
* You may also track events with additional yet optional data.
*
* @param name event name to be tracked
* @param data (Optional) data to be sent with event
*/
static track(name: string, data: Object) {
CustomerioReactnative.track(name, data);
}
/**
* Use this function to send custom device attributes
* such as app preferences, timezone etc
*
* @param data device attributes data
*/
static setDeviceAttributes(data: Object) {
CustomerioReactnative.setDeviceAttributes(data);
}
/**
* Set custom user profile information such as user preference, specific
* user actions etc
*
* @param data additional attributes for a user profile
*/
static setProfileAttributes(data: Object) {
CustomerioReactnative.setProfileAttributes(data);
}
/**
* Track screen events to record the screens a user visits
*
* @param name name of the screen user visited
* @param data (Optional) any additional data to be sent
*/
static screen(name: string, data: Object) {
CustomerioReactnative.screen(name, data);
}
}
export { CustomerIO, Region };