posthog-js-lite 0.0.4 → 2.0.0-alpha2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -14
- package/index.ts +5 -0
- package/lib/index.cjs.js +1473 -0
- package/lib/index.cjs.js.map +1 -0
- package/lib/index.d.ts +177 -0
- package/lib/index.esm.js +1468 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/node_modules/tslib/tslib.es6.d.ts +35 -0
- package/lib/posthog-core/src/eventemitter.d.ts +8 -0
- package/lib/posthog-core/src/index.d.ts +84 -0
- package/lib/posthog-core/src/lz-string.d.ts +8 -0
- package/lib/posthog-core/src/types.d.ts +68 -0
- package/lib/posthog-core/src/utils.d.ts +13 -0
- package/lib/posthog-web/index.d.ts +3 -0
- package/lib/posthog-web/src/context.d.ts +1 -0
- package/lib/posthog-web/src/posthog-web.d.ts +18 -0
- package/lib/posthog-web/src/storage.d.ts +12 -0
- package/package.json +5 -42
- package/src/context.ts +168 -0
- package/src/posthog-web.ts +70 -0
- package/src/storage.ts +115 -0
- package/test/posthog-web.spec.ts +32 -0
- package/tsconfig.json +4 -17
- package/.prettierrc +0 -7
- package/babel.config.js +0 -4
- package/dist/babel.config.d.ts +0 -5
- package/dist/babel.config.js +0 -5
- package/dist/babel.config.js.map +0 -1
- package/dist/package.json +0 -44
- package/dist/src/__tests__/index.d.ts +0 -1
- package/dist/src/__tests__/index.js +0 -48
- package/dist/src/__tests__/index.js.map +0 -1
- package/dist/src/index.d.ts +0 -19
- package/dist/src/index.js +0 -109
- package/dist/src/index.js.map +0 -1
- package/dist/src/targets/browser.d.ts +0 -19
- package/dist/src/targets/browser.js +0 -10
- package/dist/src/targets/browser.js.map +0 -1
- package/dist/src/targets/node.d.ts +0 -19
- package/dist/src/targets/node.js +0 -9
- package/dist/src/targets/node.js.map +0 -1
- package/dist/src/types.d.ts +0 -20
- package/dist/src/types.js +0 -3
- package/dist/src/types.js.map +0 -1
- package/dist/src/utils/context.d.ts +0 -18
- package/dist/src/utils/context.js +0 -182
- package/dist/src/utils/context.js.map +0 -1
- package/dist/src/utils/lz-string.d.ts +0 -14
- package/dist/src/utils/lz-string.js +0 -467
- package/dist/src/utils/lz-string.js.map +0 -1
- package/dist/src/utils/utils.d.ts +0 -3
- package/dist/src/utils/utils.js +0 -29
- package/dist/src/utils/utils.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -2454
- package/src/__tests__/index.js +0 -47
- package/src/index.ts +0 -127
- package/src/targets/browser.ts +0 -17
- package/src/targets/node.ts +0 -6
- package/src/types.ts +0 -22
- package/src/utils/context.ts +0 -158
- package/src/utils/lz-string.js +0 -511
- package/src/utils/utils.ts +0 -27
package/src/context.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { utils } from '../../posthog-core'
|
|
2
|
+
// import { version } from '../package.json'
|
|
3
|
+
|
|
4
|
+
// TODO: Get this from package.json
|
|
5
|
+
const version = '2.0.0-alpha'
|
|
6
|
+
|
|
7
|
+
export function getContext(window: Window): any {
|
|
8
|
+
let context = {}
|
|
9
|
+
if (window.navigator) {
|
|
10
|
+
const userAgent = window.navigator.userAgent
|
|
11
|
+
context = {
|
|
12
|
+
...context,
|
|
13
|
+
$os: os(window),
|
|
14
|
+
$browser: browser(userAgent, window.navigator.vendor, !!(window as any).opera),
|
|
15
|
+
$referrer: window.document.referrer,
|
|
16
|
+
$referring_domain: referringDomain(window.document.referrer),
|
|
17
|
+
$device: device(userAgent),
|
|
18
|
+
$current_url: window.location.href,
|
|
19
|
+
$host: window.location.host,
|
|
20
|
+
$pathname: window.location.pathname,
|
|
21
|
+
$browser_version: browserVersion(userAgent, window.navigator.vendor, !!(window as any).opera),
|
|
22
|
+
$screen_height: window.screen.height,
|
|
23
|
+
$screen_width: window.screen.width,
|
|
24
|
+
$screen_dpr: window.devicePixelRatio,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
context = {
|
|
28
|
+
...context,
|
|
29
|
+
$lib: 'js',
|
|
30
|
+
$lib_version: version,
|
|
31
|
+
$insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
|
|
32
|
+
$time: utils.currentTimestamp() / 1000, // epoch time in seconds
|
|
33
|
+
}
|
|
34
|
+
return context // TODO: strip empty props?
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function includes(haystack: string, needle: string): boolean {
|
|
38
|
+
return haystack.indexOf(needle) >= 0
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function browser(userAgent: string, vendor: string, opera: boolean): string {
|
|
42
|
+
vendor = vendor || '' // vendor is undefined for at least IE9
|
|
43
|
+
if (opera || includes(userAgent, ' OPR/')) {
|
|
44
|
+
if (includes(userAgent, 'Mini')) {
|
|
45
|
+
return 'Opera Mini'
|
|
46
|
+
}
|
|
47
|
+
return 'Opera'
|
|
48
|
+
} else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
|
|
49
|
+
return 'BlackBerry'
|
|
50
|
+
} else if (includes(userAgent, 'IEMobile') || includes(userAgent, 'WPDesktop')) {
|
|
51
|
+
return 'Internet Explorer Mobile'
|
|
52
|
+
} else if (includes(userAgent, 'SamsungBrowser/')) {
|
|
53
|
+
// https://developer.samsung.com/internet/user-agent-string-format
|
|
54
|
+
return 'Samsung Internet'
|
|
55
|
+
} else if (includes(userAgent, 'Edge') || includes(userAgent, 'Edg/')) {
|
|
56
|
+
return 'Microsoft Edge'
|
|
57
|
+
} else if (includes(userAgent, 'FBIOS')) {
|
|
58
|
+
return 'Facebook Mobile'
|
|
59
|
+
} else if (includes(userAgent, 'Chrome')) {
|
|
60
|
+
return 'Chrome'
|
|
61
|
+
} else if (includes(userAgent, 'CriOS')) {
|
|
62
|
+
return 'Chrome iOS'
|
|
63
|
+
} else if (includes(userAgent, 'UCWEB') || includes(userAgent, 'UCBrowser')) {
|
|
64
|
+
return 'UC Browser'
|
|
65
|
+
} else if (includes(userAgent, 'FxiOS')) {
|
|
66
|
+
return 'Firefox iOS'
|
|
67
|
+
} else if (includes(vendor, 'Apple')) {
|
|
68
|
+
if (includes(userAgent, 'Mobile')) {
|
|
69
|
+
return 'Mobile Safari'
|
|
70
|
+
}
|
|
71
|
+
return 'Safari'
|
|
72
|
+
} else if (includes(userAgent, 'Android')) {
|
|
73
|
+
return 'Android Mobile'
|
|
74
|
+
} else if (includes(userAgent, 'Konqueror')) {
|
|
75
|
+
return 'Konqueror'
|
|
76
|
+
} else if (includes(userAgent, 'Firefox')) {
|
|
77
|
+
return 'Firefox'
|
|
78
|
+
} else if (includes(userAgent, 'MSIE') || includes(userAgent, 'Trident/')) {
|
|
79
|
+
return 'Internet Explorer'
|
|
80
|
+
} else if (includes(userAgent, 'Gecko')) {
|
|
81
|
+
return 'Mozilla'
|
|
82
|
+
} else {
|
|
83
|
+
return ''
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function browserVersion(userAgent: string, vendor: string, opera: boolean): number | null {
|
|
88
|
+
const regexList = {
|
|
89
|
+
'Internet Explorer Mobile': /rv:(\d+(\.\d+)?)/,
|
|
90
|
+
'Microsoft Edge': /Edge?\/(\d+(\.\d+)?)/,
|
|
91
|
+
Chrome: /Chrome\/(\d+(\.\d+)?)/,
|
|
92
|
+
'Chrome iOS': /CriOS\/(\d+(\.\d+)?)/,
|
|
93
|
+
'UC Browser': /(UCBrowser|UCWEB)\/(\d+(\.\d+)?)/,
|
|
94
|
+
Safari: /Version\/(\d+(\.\d+)?)/,
|
|
95
|
+
'Mobile Safari': /Version\/(\d+(\.\d+)?)/,
|
|
96
|
+
Opera: /(Opera|OPR)\/(\d+(\.\d+)?)/,
|
|
97
|
+
Firefox: /Firefox\/(\d+(\.\d+)?)/,
|
|
98
|
+
'Firefox iOS': /FxiOS\/(\d+(\.\d+)?)/,
|
|
99
|
+
Konqueror: /Konqueror:(\d+(\.\d+)?)/,
|
|
100
|
+
BlackBerry: /BlackBerry (\d+(\.\d+)?)/,
|
|
101
|
+
'Android Mobile': /android\s(\d+(\.\d+)?)/,
|
|
102
|
+
'Samsung Internet': /SamsungBrowser\/(\d+(\.\d+)?)/,
|
|
103
|
+
'Internet Explorer': /(rv:|MSIE )(\d+(\.\d+)?)/,
|
|
104
|
+
Mozilla: /rv:(\d+(\.\d+)?)/,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const browserString = browser(userAgent, vendor, opera) as keyof typeof regexList
|
|
108
|
+
const regex: RegExp = regexList[browserString] || undefined
|
|
109
|
+
|
|
110
|
+
if (regex === undefined) {
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
const matches = userAgent.match(regex)
|
|
114
|
+
if (!matches) {
|
|
115
|
+
return null
|
|
116
|
+
}
|
|
117
|
+
return parseFloat(matches[matches.length - 2])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function os(window: Window): string {
|
|
121
|
+
const a = window.navigator.userAgent
|
|
122
|
+
if (/Windows/i.test(a)) {
|
|
123
|
+
if (/Phone/.test(a) || /WPDesktop/.test(a)) {
|
|
124
|
+
return 'Windows Phone'
|
|
125
|
+
}
|
|
126
|
+
return 'Windows'
|
|
127
|
+
} else if (/(iPhone|iPad|iPod)/.test(a)) {
|
|
128
|
+
return 'iOS'
|
|
129
|
+
} else if (/Android/.test(a)) {
|
|
130
|
+
return 'Android'
|
|
131
|
+
} else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {
|
|
132
|
+
return 'BlackBerry'
|
|
133
|
+
} else if (/Mac/i.test(a)) {
|
|
134
|
+
return 'Mac OS X'
|
|
135
|
+
} else if (/Linux/.test(a)) {
|
|
136
|
+
return 'Linux'
|
|
137
|
+
} else if (/CrOS/.test(a)) {
|
|
138
|
+
return 'Chrome OS'
|
|
139
|
+
} else {
|
|
140
|
+
return ''
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function device(userAgent: string): string {
|
|
145
|
+
if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {
|
|
146
|
+
return 'Windows Phone'
|
|
147
|
+
} else if (/iPad/.test(userAgent)) {
|
|
148
|
+
return 'iPad'
|
|
149
|
+
} else if (/iPod/.test(userAgent)) {
|
|
150
|
+
return 'iPod Touch'
|
|
151
|
+
} else if (/iPhone/.test(userAgent)) {
|
|
152
|
+
return 'iPhone'
|
|
153
|
+
} else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
|
|
154
|
+
return 'BlackBerry'
|
|
155
|
+
} else if (/Android/.test(userAgent)) {
|
|
156
|
+
return 'Android'
|
|
157
|
+
} else {
|
|
158
|
+
return ''
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function referringDomain(referrer: string): string {
|
|
163
|
+
const split = referrer.split('/')
|
|
164
|
+
if (split.length >= 3) {
|
|
165
|
+
return split[2]
|
|
166
|
+
}
|
|
167
|
+
return ''
|
|
168
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PostHogCore,
|
|
3
|
+
PosthogCoreOptions,
|
|
4
|
+
PostHogFetchOptions,
|
|
5
|
+
PostHogFetchResponse,
|
|
6
|
+
PostHogPersistedProperty,
|
|
7
|
+
} from '../../posthog-core/src'
|
|
8
|
+
import { getContext } from './context'
|
|
9
|
+
import { localStore, cookieStore, sessionStorage } from './storage'
|
|
10
|
+
import { version } from '../package.json'
|
|
11
|
+
|
|
12
|
+
export interface PostHogOptions extends PosthogCoreOptions {
|
|
13
|
+
autocapture?: boolean
|
|
14
|
+
persistence_name?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class PostHog extends PostHogCore {
|
|
18
|
+
private _storage = localStore || sessionStorage || cookieStore
|
|
19
|
+
private _storageCache: any
|
|
20
|
+
private _storageKey: string
|
|
21
|
+
|
|
22
|
+
constructor(apiKey: string, options?: PostHogOptions) {
|
|
23
|
+
super(apiKey, options)
|
|
24
|
+
|
|
25
|
+
// posthog-js stores options in one object on
|
|
26
|
+
this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
|
|
30
|
+
if (!this._storageCache) {
|
|
31
|
+
this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return this._storageCache[key]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void {
|
|
38
|
+
if (!this._storageCache) {
|
|
39
|
+
this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (value === null) {
|
|
43
|
+
delete this._storageCache[key]
|
|
44
|
+
this._storage.removeItem(this._storageKey)
|
|
45
|
+
} else {
|
|
46
|
+
this._storageCache[key] = value
|
|
47
|
+
this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
|
|
52
|
+
return window.fetch(url, options)
|
|
53
|
+
}
|
|
54
|
+
getLibraryId(): string {
|
|
55
|
+
return 'posthog-web'
|
|
56
|
+
}
|
|
57
|
+
getLibraryVersion(): string {
|
|
58
|
+
return version
|
|
59
|
+
}
|
|
60
|
+
getCustomUserAgent(): void {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getCommonEventProperties(): any {
|
|
65
|
+
return {
|
|
66
|
+
...super.getCommonEventProperties(),
|
|
67
|
+
...getContext(window),
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
export type PostHogStorage = {
|
|
2
|
+
getItem: (key: string) => string | null | undefined
|
|
3
|
+
setItem: (key: string, value: string) => void
|
|
4
|
+
removeItem: (key: string) => void
|
|
5
|
+
clear: () => void
|
|
6
|
+
getAllKeys: () => readonly string[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Methods partially borrowed from quirksmode.org/js/cookies.html
|
|
10
|
+
export const cookieStore: PostHogStorage = {
|
|
11
|
+
getItem(key) {
|
|
12
|
+
try {
|
|
13
|
+
const nameEQ = key + '='
|
|
14
|
+
const ca = document.cookie.split(';')
|
|
15
|
+
for (let i = 0; i < ca.length; i++) {
|
|
16
|
+
let c = ca[i]
|
|
17
|
+
while (c.charAt(0) == ' ') {
|
|
18
|
+
c = c.substring(1, c.length)
|
|
19
|
+
}
|
|
20
|
+
if (c.indexOf(nameEQ) === 0) {
|
|
21
|
+
return decodeURIComponent(c.substring(nameEQ.length, c.length))
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} catch (err) {}
|
|
25
|
+
return null
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
setItem(key: string, value: string) {
|
|
29
|
+
try {
|
|
30
|
+
const cdomain = '',
|
|
31
|
+
expires = '',
|
|
32
|
+
secure = ''
|
|
33
|
+
|
|
34
|
+
const new_cookie_val =
|
|
35
|
+
key + '=' + encodeURIComponent(JSON.stringify(value)) + expires + '; path=/' + cdomain + secure
|
|
36
|
+
document.cookie = new_cookie_val
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
removeItem(name) {
|
|
43
|
+
try {
|
|
44
|
+
cookieStore.setItem(name, '')
|
|
45
|
+
} catch (err) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
clear() {
|
|
50
|
+
document.cookie = ''
|
|
51
|
+
},
|
|
52
|
+
getAllKeys() {
|
|
53
|
+
const ca = document.cookie.split(';')
|
|
54
|
+
const keys = []
|
|
55
|
+
|
|
56
|
+
for (let i = 0; i < ca.length; i++) {
|
|
57
|
+
let c = ca[i]
|
|
58
|
+
while (c.charAt(0) == ' ') {
|
|
59
|
+
c = c.substring(1, c.length)
|
|
60
|
+
}
|
|
61
|
+
keys.push(c.split('=')[0])
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return keys
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const createStorageLike = (store: any): PostHogStorage => {
|
|
69
|
+
return {
|
|
70
|
+
getItem(key) {
|
|
71
|
+
return store.getItem(key)
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
setItem(key, value) {
|
|
75
|
+
store.setItem(key, JSON.stringify(value))
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
removeItem(key) {
|
|
79
|
+
store.removeItem(key)
|
|
80
|
+
},
|
|
81
|
+
clear() {
|
|
82
|
+
store.clear()
|
|
83
|
+
},
|
|
84
|
+
getAllKeys() {
|
|
85
|
+
const keys = []
|
|
86
|
+
for (const key in localStorage) {
|
|
87
|
+
keys.push(key)
|
|
88
|
+
}
|
|
89
|
+
return keys
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const _localStore = createStorageLike(window.localStorage)
|
|
95
|
+
export const _sessionStore = createStorageLike(window.sessionStorage)
|
|
96
|
+
|
|
97
|
+
const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {
|
|
98
|
+
if (!window) {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
const val = 'xyz'
|
|
103
|
+
storage.setItem(key, val)
|
|
104
|
+
if (storage.getItem(key) !== val) {
|
|
105
|
+
return false
|
|
106
|
+
}
|
|
107
|
+
storage.removeItem(key)
|
|
108
|
+
return true
|
|
109
|
+
} catch (err) {
|
|
110
|
+
return false
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export const localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined
|
|
115
|
+
export const sessionStorage = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { PostHog } from '..'
|
|
6
|
+
|
|
7
|
+
describe('PosthogWeb', () => {
|
|
8
|
+
let fetch: jest.Mock
|
|
9
|
+
jest.useFakeTimers()
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
;(global as any).window.fetch = fetch = jest.fn(() =>
|
|
13
|
+
Promise.resolve({
|
|
14
|
+
status: 200,
|
|
15
|
+
json: () => Promise.resolve({ status: 'ok' }),
|
|
16
|
+
})
|
|
17
|
+
) as any
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe('init', () => {
|
|
21
|
+
it('should initialise', () => {
|
|
22
|
+
const postHog = new PostHog('TEST_API_KEY', {
|
|
23
|
+
flushAt: 1,
|
|
24
|
+
})
|
|
25
|
+
expect(postHog.optedOut).toEqual(false)
|
|
26
|
+
|
|
27
|
+
postHog.capture('test')
|
|
28
|
+
|
|
29
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
})
|
package/tsconfig.json
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"emitDecoratorMetadata": true,
|
|
7
|
-
"experimentalDecorators": true,
|
|
8
|
-
"resolveJsonModule": true,
|
|
9
|
-
"allowJs": true,
|
|
10
|
-
"target": "es6",
|
|
11
|
-
"sourceMap": true,
|
|
12
|
-
"outDir": "./dist",
|
|
13
|
-
"baseUrl": "./src",
|
|
14
|
-
"incremental": true,
|
|
15
|
-
"types": ["jest", "node"],
|
|
16
|
-
"strict": true
|
|
17
|
-
},
|
|
18
|
-
"exclude": ["node_modules", "dist"]
|
|
2
|
+
"extends": "../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"lib": ["DOM"]
|
|
5
|
+
}
|
|
19
6
|
}
|
package/.prettierrc
DELETED
package/babel.config.js
DELETED
package/dist/babel.config.d.ts
DELETED
package/dist/babel.config.js
DELETED
package/dist/babel.config.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"babel.config.js","sourceRoot":"","sources":["../babel.config.js"],"names":[],"mappings":";AACA,MAAM,CAAC,OAAO,GAAG;IACb,SAAS,EAAE,CAAC,CAAC,mBAAmB,EAAE,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,CAAC,EAAE,0BAA0B,CAAC;CACvG,CAAA"}
|
package/dist/package.json
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "posthog-js-lite",
|
|
3
|
-
"version": "0.0.4",
|
|
4
|
-
"description": "Reimplementation of posthog-js to be as light and modular as possible.",
|
|
5
|
-
"main": "dist/src/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"build": "tsc -p .",
|
|
8
|
-
"test": "jest",
|
|
9
|
-
"prepublish": "yarn test && yarn build"
|
|
10
|
-
},
|
|
11
|
-
"author": "PostHog",
|
|
12
|
-
"license": "MIT",
|
|
13
|
-
"devDependencies": {
|
|
14
|
-
"@babel/core": "^7.10.4",
|
|
15
|
-
"@babel/preset-env": "^7.10.4",
|
|
16
|
-
"@babel/preset-typescript": "^7.10.4",
|
|
17
|
-
"@types/jest": "^26.0.3",
|
|
18
|
-
"husky": ">=4",
|
|
19
|
-
"lint-staged": ">=10",
|
|
20
|
-
"@types/node": "^14.0.19",
|
|
21
|
-
"prettier": "^2.0.5",
|
|
22
|
-
"eslint": "^7.4.0",
|
|
23
|
-
"eslint-config-prettier": "^6.11.0",
|
|
24
|
-
"eslint-plugin-prettier": "^3.1.4",
|
|
25
|
-
"babel-jest": "^26.1.0",
|
|
26
|
-
"jest": "^26.1.0",
|
|
27
|
-
"ts-jest": "^26.1.1",
|
|
28
|
-
"ts-node": "^8.10.2",
|
|
29
|
-
"typescript": "^3.9.6"
|
|
30
|
-
},
|
|
31
|
-
"jest": {
|
|
32
|
-
"testPathIgnorePatterns": [
|
|
33
|
-
"<rootDir>/dist/"
|
|
34
|
-
]
|
|
35
|
-
},
|
|
36
|
-
"husky": {
|
|
37
|
-
"hooks": {
|
|
38
|
-
"pre-commit": "lint-staged"
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
"lint-staged": {
|
|
42
|
-
"*.{ts,js,css,scss}": "prettier --write"
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const index_1 = require("../index");
|
|
13
|
-
const lz_string_1 = require("../utils/lz-string");
|
|
14
|
-
const testWindow = {
|
|
15
|
-
navigator: {
|
|
16
|
-
userAgent: 'not me / Mozilla',
|
|
17
|
-
},
|
|
18
|
-
document: {
|
|
19
|
-
referrer: '',
|
|
20
|
-
},
|
|
21
|
-
location: {
|
|
22
|
-
href: 'http://example.com/',
|
|
23
|
-
host: 'example.com',
|
|
24
|
-
pathname: '/',
|
|
25
|
-
},
|
|
26
|
-
screen: {
|
|
27
|
-
width: 640,
|
|
28
|
-
height: 480,
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
test('capture makes a window.fetch call', () => {
|
|
32
|
-
expect(true).toBe(true);
|
|
33
|
-
const fetchCalled = [];
|
|
34
|
-
const postHog = index_1.createInternalPostHogInstance('API_KEY_WAS_HERE', {
|
|
35
|
-
fetch: (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
-
fetchCalled.push({ url, options });
|
|
37
|
-
}),
|
|
38
|
-
}, testWindow);
|
|
39
|
-
postHog.capture('hi there!');
|
|
40
|
-
expect(fetchCalled.length).toBe(1);
|
|
41
|
-
expect(fetchCalled[0].url).toMatch(/^https:\/\/app\.posthog\.com\/e\/\?ip=1&_=[0-9]+&v=[0-9\.a-z\-]+$/);
|
|
42
|
-
expect(fetchCalled[0].options.method).toBe('POST');
|
|
43
|
-
const bodyText = decodeURIComponent(fetchCalled[0].options.body.split('&')[0].split('=')[1]);
|
|
44
|
-
const body = JSON.parse(lz_string_1.LZString.decompressFromBase64(bodyText));
|
|
45
|
-
expect(body.api_key).toBe('API_KEY_WAS_HERE');
|
|
46
|
-
expect(body.batch[0].event).toBe('hi there!');
|
|
47
|
-
});
|
|
48
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/__tests__/index.js"],"names":[],"mappings":";;;;;;;;;;;AAAA,oCAAwD;AACxD,kDAA6C;AAE7C,MAAM,UAAU,GAAG;IACf,SAAS,EAAE;QACP,SAAS,EAAE,kBAAkB;KAChC;IACD,QAAQ,EAAE;QACN,QAAQ,EAAE,EAAE;KACf;IACD,QAAQ,EAAE;QACN,IAAI,EAAE,qBAAqB;QAC3B,IAAI,EAAE,aAAa;QACnB,QAAQ,EAAE,GAAG;KAChB;IACD,MAAM,EAAE;QACJ,KAAK,EAAE,GAAG;QACV,M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AACjD,CAAC,CAAC,CAAA"}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { PostHogAPIRequest, PostHogOptions, PostHogSession } from './types';
|
|
2
|
-
export declare function createInternalPostHogInstance(apiKey: string, options: PostHogOptions, globalThis: any): {
|
|
3
|
-
options: Required<PostHogOptions>;
|
|
4
|
-
session: PostHogSession;
|
|
5
|
-
getDistinctId(): string;
|
|
6
|
-
getContextProperties(): {
|
|
7
|
-
$lib: string;
|
|
8
|
-
$lib_version: string;
|
|
9
|
-
};
|
|
10
|
-
optedIn(): boolean;
|
|
11
|
-
optIn(): void;
|
|
12
|
-
optOut(): void;
|
|
13
|
-
capture(event: string, properties?: {}): void;
|
|
14
|
-
identify(distinctId: string | null, userProperties?: {}): void;
|
|
15
|
-
queue: PostHogAPIRequest[];
|
|
16
|
-
enqueue(apiRequest: PostHogAPIRequest): void;
|
|
17
|
-
flush(): void;
|
|
18
|
-
makeRequest: (events: PostHogAPIRequest[]) => Promise<void>;
|
|
19
|
-
};
|
package/dist/src/index.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.createInternalPostHogInstance = void 0;
|
|
13
|
-
const package_json_1 = require("../package.json");
|
|
14
|
-
const utils_1 = require("./utils/utils");
|
|
15
|
-
const context_1 = require("./utils/context");
|
|
16
|
-
const lz_string_js_1 = require("./utils/lz-string.js");
|
|
17
|
-
const defaultOptions = {
|
|
18
|
-
apiHost: 'https://app.posthog.com',
|
|
19
|
-
maxQueueLength: 1,
|
|
20
|
-
optedIn: true,
|
|
21
|
-
};
|
|
22
|
-
function createInternalPostHogInstance(apiKey, options, globalThis) {
|
|
23
|
-
const session = {};
|
|
24
|
-
const anonymousId = utils_1.generateUUID();
|
|
25
|
-
let postHogInstance = {
|
|
26
|
-
options: Object.assign(Object.assign(Object.assign({}, defaultOptions), options), { apiKey }),
|
|
27
|
-
session: Object.assign({ anonymousId: anonymousId, distinctId: anonymousId }, session),
|
|
28
|
-
getDistinctId() {
|
|
29
|
-
return postHogInstance.session.distinctId;
|
|
30
|
-
},
|
|
31
|
-
getContextProperties() {
|
|
32
|
-
return {
|
|
33
|
-
$lib: 'posthog-js-lite',
|
|
34
|
-
$lib_version: package_json_1.version,
|
|
35
|
-
};
|
|
36
|
-
},
|
|
37
|
-
optedIn() {
|
|
38
|
-
return postHogInstance.options.optedIn;
|
|
39
|
-
},
|
|
40
|
-
optIn() {
|
|
41
|
-
postHogInstance.options.optedIn = true;
|
|
42
|
-
postHogInstance.flush();
|
|
43
|
-
},
|
|
44
|
-
optOut() {
|
|
45
|
-
postHogInstance.options.optedIn = false;
|
|
46
|
-
},
|
|
47
|
-
capture(event, properties = {}) {
|
|
48
|
-
postHogInstance.enqueue({
|
|
49
|
-
event,
|
|
50
|
-
distinct_id: postHogInstance.getDistinctId(),
|
|
51
|
-
timestamp: utils_1.currentISOTime(),
|
|
52
|
-
properties: Object.assign(Object.assign({}, context_1.getContext(globalThis)), properties),
|
|
53
|
-
});
|
|
54
|
-
},
|
|
55
|
-
identify(distinctId, userProperties = {}) {
|
|
56
|
-
postHogInstance.enqueue({
|
|
57
|
-
event: '$identify',
|
|
58
|
-
distinct_id: distinctId || postHogInstance.session.anonymousId,
|
|
59
|
-
timestamp: utils_1.currentISOTime(),
|
|
60
|
-
$set: Object.assign({}, userProperties),
|
|
61
|
-
properties: Object.assign(Object.assign({}, context_1.getContext(globalThis)), { $anon_distinct_id: postHogInstance.session.anonymousId }),
|
|
62
|
-
});
|
|
63
|
-
if (distinctId) {
|
|
64
|
-
postHogInstance.session.distinctId = distinctId;
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
queue: [],
|
|
68
|
-
enqueue(apiRequest) {
|
|
69
|
-
postHogInstance.queue.push(apiRequest);
|
|
70
|
-
if (postHogInstance.optedIn() && postHogInstance.queue.length >= postHogInstance.options.maxQueueLength) {
|
|
71
|
-
postHogInstance.flush();
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
flush() {
|
|
75
|
-
let queue = postHogInstance.queue;
|
|
76
|
-
postHogInstance.queue = [];
|
|
77
|
-
postHogInstance.makeRequest(queue);
|
|
78
|
-
},
|
|
79
|
-
makeRequest: function (events) {
|
|
80
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
const requestData = {
|
|
82
|
-
api_key: postHogInstance.options.apiKey,
|
|
83
|
-
batch: events,
|
|
84
|
-
sent_at: utils_1.currentISOTime(),
|
|
85
|
-
};
|
|
86
|
-
const url = `${postHogInstance.options.apiHost}/e/?ip=1&_=${utils_1.currentTimestamp()}&v=${package_json_1.version}`;
|
|
87
|
-
const payload = JSON.stringify(requestData);
|
|
88
|
-
const compressedPayload = lz_string_js_1.LZString.compressToBase64(payload);
|
|
89
|
-
const fetchOptions = {
|
|
90
|
-
method: 'POST',
|
|
91
|
-
mode: 'no-cors',
|
|
92
|
-
credentials: 'omit',
|
|
93
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
94
|
-
body: `data=${encodeURIComponent(compressedPayload)}&compression=lz64`,
|
|
95
|
-
};
|
|
96
|
-
try {
|
|
97
|
-
const rawResponse = yield postHogInstance.options.fetch(url, fetchOptions);
|
|
98
|
-
const body = yield rawResponse.text();
|
|
99
|
-
}
|
|
100
|
-
catch (error) {
|
|
101
|
-
throw error;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
return postHogInstance;
|
|
107
|
-
}
|
|
108
|
-
exports.createInternalPostHogInstance = createInternalPostHogInstance;
|
|
109
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAyC;AAEzC,yCAA8E;AAC9E,6CAA4C;AAE5C,uDAA+C;AAE/C,MAAM,cAAc,GAAmB;IACnC,OAAO,EAAE,yBAAyB;IAClC,cAAc,EAAE,CAAC;IACjB,OAAO,EAAE,IAAI;CAChB,CAAA;AAED,SAAgB,6BAA6B,CAAC,MAAc,EAAE,OAAuB,EAAE,UAAe;IAClG,MAAM,OAAO,GAAG,EAA6B,CAAA;IAC7C,MAAM,WAAW,GAAG,oBAAY,EAAE,CAAA;IAElC,IAAI,eAAe,GAAG;QAClB,OAAO,EAAE,8CAAK,cAAc,GAAK,OAAO,KAAE,MAAM,GAA8B;QAE9E,OAAO,EAAE,gBACL,WAAW,EAAE,WAAW,EACxB,UAAU,EAAE,WAAW,IACpB,OAAO,CACK;QAEnB,aAAa;YACT,OAAO,eAAe,CAAC,OAAO,CAAC,UAAU,CAAA;QAC7C,CAAC;QAED,oBAAoB;YAChB,OAAO;gBACH,IAAI,EAAE,iBAAiB;gBACvB,YAAY,EAAE,sBAAO;aACxB,CAAA;QACL,CAAC;QAED,OAAO;YACH,OAAO,eAAe,CAAC,OAAO,CAAC,OAAO,CAAA;QAC1C,CAAC;QAED,KAAK;YACD,eAAe,CAAC,OAAO,CAAC,OAAO,GAAG,IAAI,CAAA;YACtC,eAAe,CAAC,KAAK,EAAE,CAAA;QAC3B,CAAC;QAED,MAAM;YACF,eAAe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