This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.tsx
149 lines (135 loc) · 4 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { NativeModules } from 'react-native';
type TrackerOptions = Partial<{
/**
* Domain part of screen view URL sent to Piwik.
*
* Defaults to package name on Android and bundle identifier on iOS.
* Note that the default differs from Piwik iOS SDK default
*/
applicationDomain: string;
/**
* Interval, in seconds, between automatic delivery of tracking events.
*
* See:
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_Android.html#dispatching
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_iOS.html#dispatching
*/
dispatchInterval: number;
/**
* If set to false, default URL path prefixes are disabled.
*
* See
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_Android.html#tracking-screen-views
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_iOS.html#tracking-screen-views
*/
isPrefixingEnabled: boolean;
}>;
export interface CustomDimensions {
[index: number]: string;
}
interface CustomDimension {
index: number;
value: string;
}
type PiwikProSdkType = {
init(baseUrl: string, siteId: string, options: TrackerOptions): Promise<void>;
trackScreen(
path: string,
customDimensions?: CustomDimension[]
): Promise<void>;
trackEvent(
category: string,
action: string,
// Optional arguments need to be passed in map
// since nullable numbers are not supported
optionalArgs: {
name?: string;
value?: number;
},
customDimensions?: CustomDimension[]
): Promise<void>;
dispatch(): Promise<void>;
};
const PiwikProSdk: PiwikProSdkType = NativeModules.PiwikProSdk;
/**
* Initialize the SDK. Needs to be called before calling tracking functions.
* The tracker can only be initialized once, subsequent calls will fail.
*
* See:
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_Android.html#configuration
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_iOS.html#configuration
*/
export async function init(
baseUrl: string,
siteId: string,
options: TrackerOptions = {}
): Promise<void> {
return await PiwikProSdk.init(baseUrl, siteId, options);
}
/**
* Track a screen view.
*
* See:
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_Android.html#tracking-screen-views
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_iOS.html#tracking-screen-views
*/
export async function trackScreen(
path: string,
customDimensions?: CustomDimensions
): Promise<void> {
const customDimensionsArray = customDimensionsToArray(customDimensions);
return await PiwikProSdk.trackScreen(path, customDimensionsArray);
}
/**
* Track a custom event.
*
* See:
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_Android.html#tracking-custom-events
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_iOS.html#tracking-custom-events
*/
export async function trackEvent(
category: string,
action: string,
name?: string,
value?: number,
customDimensions?: CustomDimensions
): Promise<void> {
const customDimensionsArray = customDimensionsToArray(customDimensions);
return await PiwikProSdk.trackEvent(
category,
action,
{
name,
value,
},
customDimensionsArray
);
}
/**
* Dispatch all queued tracking events.
*
* See:
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_Android.html#dispatching
* - https://developers.piwik.pro/en/latest/sdk/Piwik_PRO_SDK_for_iOS.html#dispatching
*/
export const dispatch = PiwikProSdk.dispatch;
const customDimensionsToArray = (
customDimensions?: CustomDimensions
): CustomDimension[] | undefined => {
if (!customDimensions) {
return customDimensions;
}
const customDimensionsArray = Object.entries(customDimensions).map(
([i, value]) => {
const index = parseInt(i, 10);
if (index <= 0) {
throw new Error('Custom dimension index must be larger than 0');
}
return {
index,
value,
};
}
);
return customDimensionsArray;
};