A lightweight google Analytics module.
- Lightweight
- TypeScript support
Most other modules (such as universal-analytics
and electron-google-analytics
) are too large (more than 1MB) after packaging due to the import of module request
, these modules also do not support TypeScript
.
npm install google-analytics-lite
new GoogleAnalytics(tid: string, cid?: string, fetcher?: typeof fetch, root?: string)
- tid: Google Analytics ID Must
- cid: Client ID (Can be generated atomically)
- fetcher: Fetch function (Default is window.fetch)
- root: (Default is https://www.google-analytics.com/collect)
import GoogleAnalytics from 'google-analytics-lite'
const ga = new GoogleAnalytics('UA-123456-1')
By edit gs.defaultValues
:
Object.assign(gs.defaultValues, {
an: 'My App',
aid: 'com.company.app'
})
ga.pageView('http://examples.com/a.html').then(console.log)
ga.event('audio', 'Play')
ga.exception('Audio play exception')
ga.social('facebook', 'like', 'http://example.com/myApp')
ga.post({ cn: 'xxx', cs: 'xxx' }) // ga.post(data: GAParameters)
If you need other protocols, please contact me through github issue! :)
import { assign, genUUID, random } from 'google-analytics-lite'
random(4) // Random string
genUUID() // Random UUID
assign(obj1, obj2) // Assign two objects
Shirasawa