Skip to content

Commit 17672d2

Browse files
zhu-xiaoweixiaoweii
and
xiaoweii
authored
feat: add preset traffic source attributes (#7)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent e5e191b commit 17672d2

File tree

6 files changed

+125
-29
lines changed

6 files changed

+125
-29
lines changed

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,33 @@ user's attribute when it changes.
113113

114114
1. Add global attributes when initializing the SDK
115115

116+
The following example code shows how to add traffic source fields as global attributes when initializing the SDK.
116117
```typescript
118+
import { ClickstreamAnalytics, Attr } from '@aws/clickstream-react-native';
117119
ClickstreamAnalytics.init({
118-
appId: "your appId",
119-
endpoint: "https://example.com/collect",
120+
appId: 'your appId',
121+
endpoint: 'https://example.com/collect',
120122
globalAttributes:{
121-
_traffic_source_medium: "Search engine",
122-
_traffic_source_name: "Summer promotion",
123-
}
123+
[Attr.TRAFFIC_SOURCE_SOURCE]: 'amazon',
124+
[Attr.TRAFFIC_SOURCE_MEDIUM]: 'cpc',
125+
[Attr.TRAFFIC_SOURCE_CAMPAIGN]: 'summer_promotion',
126+
[Attr.TRAFFIC_SOURCE_CAMPAIGN_ID]: 'summer_promotion_01',
127+
[Attr.TRAFFIC_SOURCE_TERM]: 'running_shoes',
128+
[Attr.TRAFFIC_SOURCE_CONTENT]: 'banner_ad_1',
129+
[Attr.TRAFFIC_SOURCE_CLID]: 'amazon_ad_123',
130+
[Attr.TRAFFIC_SOURCE_CLID_PLATFORM]: 'amazon_ads',
131+
[Attr.APP_INSTALL_CHANNEL]: 'amazon_store',
132+
},
124133
});
125134
```
126135

127136
2. Add global attributes after initializing the SDK
128137

129138
``` typescript
139+
import { ClickstreamAnalytics, Attr } from '@aws/clickstream-react-native';
140+
130141
ClickstreamAnalytics.setGlobalAttributes({
131-
_traffic_source_medium: "Search engine",
142+
[Attr.TRAFFIC_SOURCE_MEDIUM]: "Search engine",
132143
level: 10,
133144
});
134145
```
@@ -149,7 +160,7 @@ You can add the following code to log an event with an item.
149160
**Note: Only pipelines from version 1.1+ can handle items with custom attribute.**
150161

151162
```typescript
152-
import { ClickstreamAnalytics, Item } from '@aws/clickstream-react-native';
163+
import { ClickstreamAnalytics, Item, Attr } from '@aws/clickstream-react-native';
153164

154165
const itemBook: Item = {
155166
id: '123',
@@ -161,7 +172,8 @@ const itemBook: Item = {
161172
ClickstreamAnalytics.record({
162173
name: 'view_item',
163174
attributes: {
164-
currency: 'USD',
175+
[Attr.VALUE]: 99,
176+
[Attr.CURRENCY]: 'USD',
165177
event_category: 'recommended',
166178
},
167179
items: [itemBook],
@@ -181,13 +193,13 @@ code to record a screen view event with two attributes.
181193
on the hashcode of the current Activity or ViewController.
182194

183195
```typescript
184-
import { ClickstreamAnalytics } from '@aws/clickstream-react-native';
196+
import { ClickstreamAnalytics, Attr, Event } from '@aws/clickstream-react-native';
185197

186198
ClickstreamAnalytics.record({
187-
name: ClickstreamAnalytics.Event.SCREEN_VIEW,
199+
name: Event.SCREEN_VIEW,
188200
attributes: {
189-
[ClickstreamAnalytics.Attr.SCREEN_NAME]: 'HomeComponet',
190-
[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID]: '123adf',
201+
[Attr.SCREEN_NAME]: 'HomeComponet',
202+
[Attr.SCREEN_UNIQUE_ID]: '123adf',
191203
},
192204
});
193205
```

example/src/App.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
ScrollView,
88
SafeAreaView,
99
} from 'react-native';
10-
import { ClickstreamAnalytics, Item } from '@aws/clickstream-react-native';
10+
import {
11+
ClickstreamAnalytics,
12+
Item,
13+
Attr,
14+
Event,
15+
} from '@aws/clickstream-react-native';
1116

1217
export default function App() {
1318
const initSDK = async () => {
@@ -20,7 +25,15 @@ export default function App() {
2025
isCompressEvents: false,
2126
sessionTimeoutDuration: 30000,
2227
globalAttributes: {
23-
channel: 'Samsung',
28+
[Attr.TRAFFIC_SOURCE_SOURCE]: 'amazon',
29+
[Attr.TRAFFIC_SOURCE_MEDIUM]: 'cpc',
30+
[Attr.TRAFFIC_SOURCE_CAMPAIGN]: 'summer_promotion',
31+
[Attr.TRAFFIC_SOURCE_CAMPAIGN_ID]: 'summer_promotion_01',
32+
[Attr.TRAFFIC_SOURCE_TERM]: 'running_shoes',
33+
[Attr.TRAFFIC_SOURCE_CONTENT]: 'banner_ad_1',
34+
[Attr.TRAFFIC_SOURCE_CLID]: 'amazon_ad_123',
35+
[Attr.TRAFFIC_SOURCE_CLID_PLATFORM]: 'amazon_ads',
36+
[Attr.APP_INSTALL_CHANNEL]: 'amazon_store',
2437
Class: 5,
2538
isTrue: true,
2639
Score: 24.32,
@@ -47,10 +60,10 @@ export default function App() {
4760

4861
const recordCustomScreenViewEvents = () => {
4962
ClickstreamAnalytics.record({
50-
name: ClickstreamAnalytics.Event.SCREEN_VIEW,
63+
name: Event.SCREEN_VIEW,
5164
attributes: {
52-
[ClickstreamAnalytics.Attr.SCREEN_NAME]: 'HomeComponent',
53-
[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID]: '123adf',
65+
[Attr.SCREEN_NAME]: 'HomeComponent',
66+
[Attr.SCREEN_UNIQUE_ID]: '123adf',
5467
},
5568
});
5669
};
@@ -68,8 +81,8 @@ export default function App() {
6881
name: 'product_view',
6982
attributes: {
7083
category: 'shoes',
71-
currency: 'CNY',
72-
price: 279.9,
84+
[Attr.CURRENCY]: 'CNY',
85+
[Attr.VALUE]: 279.9,
7386
},
7487
items: [item_shoes],
7588
});
@@ -93,7 +106,7 @@ export default function App() {
93106
const setGlobalAttributes = () => {
94107
ClickstreamAnalytics.setGlobalAttributes({});
95108
ClickstreamAnalytics.setGlobalAttributes({
96-
channel: 'Samsung',
109+
[Attr.APP_INSTALL_CHANNEL]: 'amazon_store',
97110
Class: 5,
98111
isTrue: true,
99112
Score: 24.32,

src/ClickstreamAnalytics.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,18 @@ export class ClickstreamAnalytics {
105105
return obj !== undefined && obj !== null && Object.keys(obj).length > 0;
106106
}
107107

108+
/**
109+
* @deprecated The static object should not be used.
110+
* please update to: `import { Event } from '@aws/clickstream-react-native'`.
111+
*/
108112
static readonly Event = {
109113
SCREEN_VIEW: '_screen_view',
110114
};
111115

116+
/**
117+
* @deprecated The static object should not be used.
118+
* please update to: `import { Attr } from '@aws/clickstream-react-native'`.
119+
*/
112120
static readonly Attr = {
113121
SCREEN_NAME: '_screen_name',
114122
SCREEN_UNIQUE_ID: '_screen_unique_id',

src/__tests__/ClickstreamAnalytics.test.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
1111
* and limitations under the License.
1212
*/
13-
import { ClickstreamAnalytics } from '../index';
13+
import { ClickstreamAnalytics, Attr, Event } from '../index';
1414
import { NativeModules } from 'react-native';
1515

1616
jest.mock('react-native', () => {
@@ -129,18 +129,18 @@ describe('ClickstreamAnalytics test', () => {
129129

130130
test('test record custom screen view events', () => {
131131
ClickstreamAnalytics.record({
132-
name: ClickstreamAnalytics.Event.SCREEN_VIEW,
132+
name: Event.SCREEN_VIEW,
133133
attributes: {
134-
[ClickstreamAnalytics.Attr.SCREEN_NAME]: 'HomeComponent',
135-
[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID]: '123adf',
134+
[Attr.SCREEN_NAME]: 'HomeComponent',
135+
[Attr.SCREEN_UNIQUE_ID]: '123adf',
136136
},
137137
});
138138
expect(NativeModules.ClickstreamReactNative.record).toHaveBeenCalledWith(
139139
expect.objectContaining({
140-
name: ClickstreamAnalytics.Event.SCREEN_VIEW,
140+
name: Event.SCREEN_VIEW,
141141
attributes: {
142-
[ClickstreamAnalytics.Attr.SCREEN_NAME]: 'HomeComponent',
143-
[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID]: '123adf',
142+
[Attr.SCREEN_NAME]: 'HomeComponent',
143+
[Attr.SCREEN_UNIQUE_ID]: '123adf',
144144
},
145145
})
146146
);
@@ -207,6 +207,35 @@ describe('ClickstreamAnalytics test', () => {
207207
);
208208
});
209209

210+
test('test set traffic source in global attributes', () => {
211+
ClickstreamAnalytics.setGlobalAttributes({
212+
[Attr.TRAFFIC_SOURCE_SOURCE]: 'amazon',
213+
[Attr.TRAFFIC_SOURCE_MEDIUM]: 'cpc',
214+
[Attr.TRAFFIC_SOURCE_CAMPAIGN]: 'summer_promotion',
215+
[Attr.TRAFFIC_SOURCE_CAMPAIGN_ID]: 'summer_promotion_01',
216+
[Attr.TRAFFIC_SOURCE_TERM]: 'running_shoes',
217+
[Attr.TRAFFIC_SOURCE_CONTENT]: 'banner_ad_1',
218+
[Attr.TRAFFIC_SOURCE_CLID]: 'amazon_ad_123',
219+
[Attr.TRAFFIC_SOURCE_CLID_PLATFORM]: 'amazon_ads',
220+
[Attr.APP_INSTALL_CHANNEL]: 'amazon_store',
221+
});
222+
expect(
223+
NativeModules.ClickstreamReactNative.setGlobalAttributes
224+
).toHaveBeenCalledWith(
225+
expect.objectContaining({
226+
[Attr.TRAFFIC_SOURCE_SOURCE]: 'amazon',
227+
[Attr.TRAFFIC_SOURCE_MEDIUM]: 'cpc',
228+
[Attr.TRAFFIC_SOURCE_CAMPAIGN]: 'summer_promotion',
229+
[Attr.TRAFFIC_SOURCE_CAMPAIGN_ID]: 'summer_promotion_01',
230+
[Attr.TRAFFIC_SOURCE_TERM]: 'running_shoes',
231+
[Attr.TRAFFIC_SOURCE_CONTENT]: 'banner_ad_1',
232+
[Attr.TRAFFIC_SOURCE_CLID]: 'amazon_ad_123',
233+
[Attr.TRAFFIC_SOURCE_CLID_PLATFORM]: 'amazon_ads',
234+
[Attr.APP_INSTALL_CHANNEL]: 'amazon_store',
235+
})
236+
);
237+
});
238+
210239
test('test set empty global attribute', () => {
211240
ClickstreamAnalytics.setGlobalAttributes({});
212241
expect(

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
*/
1313

1414
export { ClickstreamAnalytics } from './ClickstreamAnalytics';
15-
export { Item } from './types/Analytics';
15+
export { Item, Attr, Event } from './types/Analytics';

src/types/Analytics.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,42 @@ export interface Configuration {
3030
isTrackAppExceptionEvents?: boolean;
3131
}
3232

33+
export enum Event {
34+
SCREEN_VIEW = '_screen_view',
35+
}
36+
37+
export enum Attr {
38+
SCREEN_NAME = '_screen_name',
39+
SCREEN_UNIQUE_ID = '_screen_unique_id',
40+
TRAFFIC_SOURCE_SOURCE = '_traffic_source_source',
41+
TRAFFIC_SOURCE_MEDIUM = '_traffic_source_medium',
42+
TRAFFIC_SOURCE_CAMPAIGN = '_traffic_source_campaign',
43+
TRAFFIC_SOURCE_CAMPAIGN_ID = '_traffic_source_campaign_id',
44+
TRAFFIC_SOURCE_TERM = '_traffic_source_term',
45+
TRAFFIC_SOURCE_CONTENT = '_traffic_source_content',
46+
TRAFFIC_SOURCE_CLID = '_traffic_source_clid',
47+
TRAFFIC_SOURCE_CLID_PLATFORM = '_traffic_source_clid_platform',
48+
APP_INSTALL_CHANNEL = '_app_install_channel',
49+
VALUE = '_value',
50+
CURRENCY = '_currency',
51+
}
52+
3353
export interface ClickstreamAttribute {
34-
[key: string]: string | number | boolean;
54+
[Attr.SCREEN_NAME]?: string;
55+
[Attr.SCREEN_UNIQUE_ID]?: string;
56+
[Attr.TRAFFIC_SOURCE_SOURCE]?: string;
57+
[Attr.TRAFFIC_SOURCE_MEDIUM]?: string;
58+
[Attr.TRAFFIC_SOURCE_CAMPAIGN]?: string;
59+
[Attr.TRAFFIC_SOURCE_CAMPAIGN_ID]?: string;
60+
[Attr.TRAFFIC_SOURCE_TERM]?: string;
61+
[Attr.TRAFFIC_SOURCE_CONTENT]?: string;
62+
[Attr.TRAFFIC_SOURCE_CLID]?: string;
63+
[Attr.TRAFFIC_SOURCE_CLID_PLATFORM]?: string;
64+
[Attr.APP_INSTALL_CHANNEL]?: string;
65+
[Attr.VALUE]?: number;
66+
[Attr.CURRENCY]?: string;
67+
68+
[key: string]: string | number | boolean | undefined;
3569
}
3670

3771
export interface Item {

0 commit comments

Comments
 (0)