Skip to content

Commit

Permalink
feat: add a collect basename #27 (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: chenzhouji <chenzhouji@baidu.com>
  • Loading branch information
ice-zjchen and chenzhouji committed Jun 4, 2020
1 parent 9b48c99 commit 7b8a026
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type collect = collectPageView | collectEvent;
- `browser()`:添加浏览器相关的信息,包括UA、分辨率、操作系统、浏览器版本、系统语言。这个采集仅在类型为`pageView`时才会生效。
- `context(env)`:将固定的`env`对象放到采集数据中去,常用于添加当前登录用户名、系统名称、系统版本等信息。
- `session(storageKey)`:跟踪一次用户的访问,为每一次访问生成一个唯一的标识,并存放在`sessionStorage`中,这个唯一标识会变为名为`session`的属性值。可以通过`storageKey`来自定义`sessionStorage`中对应的键名。
- `basename(prefix)`:默认采集路径信息是基于`location.pathname`的,但它并不包含basename。所以,当history设置basename时,需要使用该collect指定basename才能采集准确的路径


当希望同时使用多个`collect`函数时,可以通过`combineCollects`将它们组合成一个函数。以下代码展示了如何使用多个`collect`函数,并通过`combineCollects`将它们组合成一个:

Expand Down
12 changes: 11 additions & 1 deletion src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
holmes,
trackEvent,
trackPageView,
basename,
} from '..';

let consoleMessage = '';
Expand Down Expand Up @@ -110,7 +111,7 @@ describe('test combineCollects and combineProvider', () => {
});
})
);
const collect = combineCollects(browser(), context({env: 'test'}), session());
const collect = combineCollects(browser(), context({env: 'test'}), session(), basename('chen'));
const provider = composeProvider(print(), customProvider, installProvider, uninstallProvider);

const wrapper = mount(<TrackEventComponent collect={collect} provider={provider} />);
Expand Down Expand Up @@ -301,3 +302,12 @@ describe('test hocs', () => {
expect(mockUninstall.mock.calls.length).toEqual(1);
});
});

describe('test basename', () => {
test('should return correct pathname', () => {
const collect = basename('/hi');
const res = collect('pageView', {pathname: '/track'});
const exp = {location: {pathname: '/hi/track'}};
expect(res).toEqual(exp);
});
});
20 changes: 20 additions & 0 deletions src/collect/basename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {CollectType, Location, TrackerCollect} from '../types';

export const basename = (name: string): TrackerCollect => {
const prefix = name.startsWith('/') ? name : '/' + name;

return (type: CollectType, location?: Location) => {
if (type !== 'pageView' || !location) {
return {};
}

return {
location: {
...location,
pathname: prefix + location.pathname,
},
};
};
};

export default basename;
1 change: 1 addition & 0 deletions src/collect/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {default as basename} from './basename';
export {default as browser} from './browser';
export {default as session} from './session';
export {default as combineCollects} from './combineCollects';
Expand Down

0 comments on commit 7b8a026

Please sign in to comment.