1.0.0 • Published 1 year ago

easy-track v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

EasyTrack

comment: <> ([npm(https://img.shields.io/npm/v/easy-log-report.svg)](https://www.npmjs.com/package/easy-log-report))

comment: <> ([CI(https://github.com/zjw93615/EasyLog/actions/workflows/npmjs.yml/badge.svg?event=release)](https://github.com/zjw93615/EasyLog/actions/workflows/npmjs.yml))

comment: <> ([Coverage Status(https://coveralls.io/repos/github/zjw93615/EasyLog/badge.svg?branch=master)](https://coveralls.io/github/zjw93615/EasyLog?branch=master))

EasyTrack is a Typescript library for dealing with js error, xhr error and performance tracking.

Installation

yarn add easy-track

or

npm install easy-track

Usage

Config and Init

Create your Report log by sendBeacon function

import EasyTrack from 'easy-track'

const easyTrack = new EasyTrack({
    acceptEventType: ['onLaunch', 'onLoad', 'onUnload', 'onShow', 'request', 'onError', 'click'],
    track: ['jsError', 'performance', 'xhr'],
    sendInterval: 1000 * 30,
    sendQueueSize: 30,
    sendUrl: 'http://localhost:8080/api/log/beacon/',
    sendType: SEND_TYPE.BEACON,
    singleMode: false,
    getCurrentPage: () => window.location.href,
    getInitialEventContent: () => {
        return {
            appInfo: {
                // appID,
                // version,
                // appName,
            },
            systemInfo: {
                ua: navigator.userAgent,
                is_cookie: window.navigator.cookieEnabled ? 1 : 0,
                cookie: document.cookie || '',
                screen_height: window.screen.availHeight,
                screen_width: window.screen.availWidth,
            },
            userInfo: {
                // userId,
                // openId,
            },
        }
    },
})
easyTrack.init()

export default easyTrack

OR report log by your custom sendFn

import EasyTrack from 'easy-track'

const easyTrack = new EasyTrack({
    acceptEventType: ['onLaunch', 'onLoad', 'onUnload', 'onShow', 'request', 'onError', 'click'],
    track: ['jsError', 'performance', 'xhr'],
    sendInterval: 1000 * 30,
    sendQueueSize: 30,
    sendFn: e => {
        doReportSend(e)
        console.log('EasyLogReport', e)
    },
    singleMode: false,
    getCurrentPage: () => window.location.href,
    getInitialEventContent: () => {
        return {
            appInfo: {
                // appID,
                // version,
                // appName,
            },
            systemInfo: {
                ua: navigator.userAgent,
                is_cookie: window.navigator.cookieEnabled ? 1 : 0,
                cookie: document.cookie || '',
                screen_height: window.screen.availHeight,
                screen_width: window.screen.availWidth,
            },
            userInfo: {
                // userId,
                // openId,
            },
        }
    },
})

easyTrack.init(() => {
    console.log('EasyLogReport init!')
})

export default easyTrack

Config Props

PropertyDescriptionTypeDefault
acceptEventTypelog event types that are allowed to be reportedstring[]'onLaunch', 'onLoad', 'onUnload', 'onShow', 'request', 'onError', 'click'
trackevent types that need to be trackedstring[]'jsError', 'performance', 'xhr'
sendIntervalsend function trigger interval(millisecond)number1000 * 30
sendQueueSizethe maximum number of logs in queuenumber50
singleModeIf it`s singleMode, report log immediately | boolean | false
sendUrlreport urlnumber-
sendTypereporting modeSEND_TYPE.IMG / SEND_TYPE.BEACON-
sendFncustom sendFn, If sendFn is set, sendUrl and sendType would be remove(content: ReportContent) => void-
getCurrentPageget current page route() => string-
getInitialEventContentget the default log content() => InitialReportContent-

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT