Skip to content

Commit 4fe6272

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add auto track events
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 425cc8c commit 4fe6272

23 files changed

+693
-256
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ yarn-error.log
1818
yarn.lock
1919
*.tsbuildinfo
2020
*.tgz
21+
/src/config.ts
2122

2223
build

LICENSE.txt

Lines changed: 0 additions & 175 deletions
This file was deleted.

README.md

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npm install @awslabs/clickstream-web
1919
For GitHub repository:
2020

2121
```bash
22-
npm install github:awslabs/clickstream-web
22+
npm install github:awslabs/clickstream-web#0.1.0
2323
```
2424

2525
Note: for beta version we use GitHub repository to distribute our SDK.
@@ -33,27 +33,10 @@ import { ClickstreamAnalytics, EventMode, PageType } from 'clickstream-web';
3333
ClickstreamAnalytics.configure({
3434
appId: "your appId",
3535
endpoint: "https://example.com/collect",
36-
sendMode: EventMode.Batch,
37-
sendEventsInterval: 5000,
38-
isTrackPageViewEvents: true,
39-
pageType: PageType.SPA,
40-
isLogEvents: false,
41-
authCookie: "your auth cookie",
42-
sessionTimeoutDuration: 1800000
43-
)}
36+
)};
4437
```
4538
46-
Your `appId` and `endpoint` are already set up in it, here's an explanation of each property:
47-
48-
- **appId (Required)**: the app id of your project in control plane.
49-
- **endpoint (Required)**: the endpoint path you will upload the event to AWS server.
50-
- **sendMode**: EventMode.Immediate, EventMode.Batch, default is Immediate mode.
51-
- **sendEventsInterval**: event sending interval millisecond, works only bath send mode, the default value is `5000`
52-
- **isTrackPageViewEvents**: whether auto page view events in browser, default is `true`
53-
- **pageType**: the website type, `SPA` for single page application, `multiPageApp` for multipule page application, default is `SPA`. This attribute works only when the attribute `isTrackPageViewEvents`'s value ' is `true`.
54-
- **isLogEvents**: whether log events json, default is false.
55-
- **authCookie**: your auth cookie for AWS application load balancer auth cookie.
56-
- **sessionTimeoutDuration**: the duration for session timeout millisecond, default is 1800000
39+
Your `appId` and `endpoint` are already set up in it.
5740
5841
### Start using
5942
@@ -94,6 +77,60 @@ ClickstreamAnalytics.setUserAttribute({
9477
9578
Current login user‘s attributes will be cached in localStorage, so the next time browser open you don't need to set all user's attribute again, of course you can update the current user's attribute when it changes.
9679
80+
#### Other configuration
81+
In addition to the required `appId` and `endpoint`, you can configure other information to get more customized usage:
82+
83+
```typescript
84+
import { ClickstreamAnalytics, EventMode, PageType } from 'clickstream-web';
85+
86+
ClickstreamAnalytics.configure({
87+
appId: "your appId",
88+
endpoint: "https://example.com/collect",
89+
sendMode: EventMode.Batch,
90+
sendEventsInterval: 5000,
91+
isTrackPageViewEvents: true,
92+
isTrackClickEvents: true,
93+
isTrackSearchEvents: true,
94+
isTrackScrollEvents: true,
95+
pageType: PageType.SPA,
96+
isLogEvents: false,
97+
authCookie: "your auth cookie",
98+
sessionTimeoutDuration: 1800000,
99+
)};
100+
```
101+
102+
Here is an explanation of each property:
103+
104+
- **appId (Required)**: the app id of your project in control plane.
105+
- **endpoint (Required)**: the endpoint path you will upload the event to AWS server.
106+
- **sendMode**: EventMode.Immediate, EventMode.Batch, default is Immediate mode.
107+
- **sendEventsInterval**: event sending interval millisecond, works only bath send mode, the default value is `5000`
108+
- **isTrackPageViewEvents**: whether auto record page view events in browser, default is `true`
109+
- **isTrackClickEvents**: whether auto record link click events in browser, default is `true`
110+
- **isTrackSearchEvents**: whether auto record search result page events in browser, default is `true`
111+
- **isTrackScrollEvents**: whether auto record page scroll events in browser, default is `true`
112+
- **pageType**: the website type, `SPA` for single page application, `multiPageApp` for multiple page application, default is `SPA`. This attribute works only when the attribute `isTrackPageViewEvents`'s value is `true`.
113+
- **isLogEvents**: whether log events json, default is false.
114+
- **authCookie**: your auth cookie for AWS application load balancer auth cookie.
115+
- **sessionTimeoutDuration**: the duration for session timeout millisecond, default is 1800000
116+
117+
#### Configuration update
118+
You can update the default configuration after initializing the SDK. We now support updating the following parameters:
119+
120+
```typescript
121+
import { ClickstreamAnalytics } from 'clickstream-web';
122+
123+
ClickstreamAnalytics.updateConfigure({
124+
isLogEvents: true,
125+
authCookie: 'your auth cookie',
126+
isTrackPageViewEvents: false,
127+
isTrackClickEvents: false,
128+
isTrackScrollEvents: false,
129+
isTrackSearchEvents: false,
130+
searchKeyWords: ['product', 'class'],
131+
});
132+
```
133+
97134
## How to build&test locally
98135
99136
**Build**

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
"access": "public"
1111
},
1212
"scripts": {
13-
"build": "npm run clean && npm run build:esm && npm run build:cjs",
13+
"prebuild": "ts-node scripts/GenerateConfig.ts",
14+
"build": "npm run prebuild && npm run clean && npm run build:esm && npm run build:cjs",
1415
"build-dev": "npm run clean && npx tsc && npx webpack --config webpack.config.dev.js",
1516
"build:cjs": "npx tsc --module commonjs && webpack && webpack --config webpack.config.dev.js",
1617
"build:esm": "npx tsc --module esnext --outDir lib-esm",
1718
"format": "npx prettier --check 'src/**/*.{js,ts}'",
1819
"lint": "npx eslint src",
19-
"test": "npx jest -w 1 --coverage",
20+
"test": "npm run prebuild && npx jest -w 1 --coverage",
2021
"clean": "rimraf lib-esm lib dist",
21-
"pack": "npm pack"
22+
"pack": "npm run prebuild && npm pack"
2223
},
2324
"dependencies": {
2425
"@aws-amplify/core": "^5.5.1",

scripts/GenerateConfig.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
import fs from 'fs';
14+
import { version } from '../package.json';
15+
16+
const config = `{
17+
sdkVersion: '${version}',
18+
};
19+
`;
20+
fs.writeFileSync('./src/config.ts', `export default ${config}`);
21+
console.log(`Version ${version} written to .env file.`);

src/ClickstreamAnalytics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
ClickstreamAttribute,
1717
ClickstreamConfiguration,
1818
ClickstreamEvent,
19+
Configuration,
1920
} from './types';
2021

2122
export class ClickstreamAnalytics {
@@ -48,4 +49,8 @@ export class ClickstreamAnalytics {
4849
public static setUserAttributes(attributes: ClickstreamAttribute) {
4950
this.provider.setUserAttributes(attributes);
5051
}
52+
53+
public static updateConfigure(configure: Configuration) {
54+
this.provider.updateConfigure(configure);
55+
}
5156
}

src/network/NetRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ClickstreamContext } from '../provider';
1616
const logger = new Logger('NetRequest');
1717

1818
export class NetRequest {
19-
static readonly REQUEST_TIMEOUT = 3000;
19+
static readonly REQUEST_TIMEOUT = 10000;
2020
static readonly BATCH_REQUEST_TIMEOUT = 15000;
2121
static readonly REQUEST_RETRY_TIMES = 3;
2222
static readonly BATCH_REQUEST_RETRY_TIMES = 1;

src/provider/AnalyticsEventBuilder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { v4 as uuidV4 } from 'uuid';
1414
import { ClickstreamContext } from './ClickstreamContext';
1515
import { Event } from './Event';
1616
import { BrowserInfo } from '../browser';
17+
import config from '../config';
1718
import { Session } from '../tracker';
1819
import {
1920
AnalyticsEvent,
@@ -24,6 +25,8 @@ import {
2425
import { HashUtil } from '../util/HashUtil';
2526
import { StorageUtil } from '../util/StorageUtil';
2627

28+
const sdkVersion = config.sdkVersion;
29+
2730
export class AnalyticsEventBuilder {
2831
static async createEvent(
2932
context: ClickstreamContext,
@@ -64,7 +67,7 @@ export class AnalyticsEventBuilder {
6467
screen_height: window.innerHeight,
6568
screen_width: window.innerWidth,
6669
sdk_name: 'aws-solution-clickstream-sdk',
67-
sdk_version: process.env.VERSION ?? '',
70+
sdk_version: sdkVersion,
6871
user: userAttributes ?? {},
6972
attributes: attributes ?? {},
7073
};

0 commit comments

Comments
 (0)