-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathautocapture.ts
418 lines (362 loc) · 14.7 KB
/
autocapture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { addEventListener, each, extend } from './utils'
import {
autocaptureCompatibleElements,
getClassNames,
getDirectAndNestedSpanText,
getElementsChainString,
getEventTarget,
getSafeText,
isAngularStyleAttr,
isSensitiveElement,
makeSafeText,
shouldCaptureDomEvent,
shouldCaptureElement,
shouldCaptureValue,
splitClassString,
} from './autocapture-utils'
import RageClick from './extensions/rageclick'
import { AutocaptureConfig, COPY_AUTOCAPTURE_EVENT, EventName, Properties, RemoteConfig } from './types'
import { PostHog } from './posthog-core'
import { AUTOCAPTURE_DISABLED_SERVER_SIDE } from './constants'
import { isBoolean, isFunction, isNull, isObject } from './utils/type-utils'
import { createLogger } from './utils/logger'
import { document, window } from './utils/globals'
import { convertToURL } from './utils/request-utils'
import { isDocumentFragment, isElementNode, isTag, isTextNode } from './utils/element-utils'
import { includes } from './utils/string-utils'
const logger = createLogger('[AutoCapture]')
function limitText(length: number, text: string): string {
if (text.length > length) {
return text.slice(0, length) + '...'
}
return text
}
export function getAugmentPropertiesFromElement(elem: Element): Properties {
const shouldCaptureEl = shouldCaptureElement(elem)
if (!shouldCaptureEl) {
return {}
}
const props: Properties = {}
each(elem.attributes, function (attr: Attr) {
if (attr.name && attr.name.indexOf('data-ph-capture-attribute') === 0) {
const propertyKey = attr.name.replace('data-ph-capture-attribute-', '')
const propertyValue = attr.value
if (propertyKey && propertyValue && shouldCaptureValue(propertyValue)) {
props[propertyKey] = propertyValue
}
}
})
return props
}
export function previousElementSibling(el: Element): Element | null {
if (el.previousElementSibling) {
return el.previousElementSibling
}
let _el: Element | null = el
do {
_el = _el.previousSibling as Element | null // resolves to ChildNode->Node, which is Element's parent class
} while (_el && !isElementNode(_el))
return _el
}
export function getDefaultProperties(eventType: string): Properties {
return {
$event_type: eventType,
$ce_version: 1,
}
}
export function getPropertiesFromElement(
elem: Element,
maskAllAttributes: boolean,
maskText: boolean,
elementAttributeIgnorelist: string[] | undefined
): Properties {
const tag_name = elem.tagName.toLowerCase()
const props: Properties = {
tag_name: tag_name,
}
if (autocaptureCompatibleElements.indexOf(tag_name) > -1 && !maskText) {
if (tag_name.toLowerCase() === 'a' || tag_name.toLowerCase() === 'button') {
props['$el_text'] = limitText(1024, getDirectAndNestedSpanText(elem))
} else {
props['$el_text'] = limitText(1024, getSafeText(elem))
}
}
const classes = getClassNames(elem)
if (classes.length > 0)
props['classes'] = classes.filter(function (c) {
return c !== ''
})
// capture the deny list here because this not-a-class class makes it tricky to use this.config in the function below
each(elem.attributes, function (attr: Attr) {
// Only capture attributes we know are safe
if (isSensitiveElement(elem) && ['name', 'id', 'class', 'aria-label'].indexOf(attr.name) === -1) return
if (elementAttributeIgnorelist?.includes(attr.name)) return
if (!maskAllAttributes && shouldCaptureValue(attr.value) && !isAngularStyleAttr(attr.name)) {
let value = attr.value
if (attr.name === 'class') {
// html attributes can _technically_ contain linebreaks,
// but we're very intolerant of them in the class string,
// so we strip them.
value = splitClassString(value).join(' ')
}
props['attr__' + attr.name] = limitText(1024, value)
}
})
let nthChild = 1
let nthOfType = 1
let currentElem: Element | null = elem
while ((currentElem = previousElementSibling(currentElem))) {
// eslint-disable-line no-cond-assign
nthChild++
if (currentElem.tagName === elem.tagName) {
nthOfType++
}
}
props['nth_child'] = nthChild
props['nth_of_type'] = nthOfType
return props
}
export function autocapturePropertiesForElement(
target: Element,
{
e,
maskAllElementAttributes,
maskAllText,
elementAttributeIgnoreList,
elementsChainAsString,
}: {
e: Event
maskAllElementAttributes: boolean
maskAllText: boolean
elementAttributeIgnoreList?: string[] | undefined
elementsChainAsString: boolean
}
): { props: Properties; explicitNoCapture?: boolean } {
const targetElementList = [target]
let curEl = target
while (curEl.parentNode && !isTag(curEl, 'body')) {
if (isDocumentFragment(curEl.parentNode)) {
targetElementList.push((curEl.parentNode as any).host)
curEl = (curEl.parentNode as any).host
continue
}
targetElementList.push(curEl.parentNode as Element)
curEl = curEl.parentNode as Element
}
const elementsJson: Properties[] = []
const autocaptureAugmentProperties: Properties = {}
let href: string | false = false
let explicitNoCapture = false
each(targetElementList, (el) => {
const shouldCaptureEl = shouldCaptureElement(el)
// if the element or a parent element is an anchor tag
// include the href as a property
if (el.tagName.toLowerCase() === 'a') {
href = el.getAttribute('href')
href = shouldCaptureEl && href && shouldCaptureValue(href) && href
}
// allow users to programmatically prevent capturing of elements by adding class 'ph-no-capture'
const classes = getClassNames(el)
if (includes(classes, 'ph-no-capture')) {
explicitNoCapture = true
}
elementsJson.push(
getPropertiesFromElement(el, maskAllElementAttributes, maskAllText, elementAttributeIgnoreList)
)
const augmentProperties = getAugmentPropertiesFromElement(el)
extend(autocaptureAugmentProperties, augmentProperties)
})
if (explicitNoCapture) {
return { props: {}, explicitNoCapture }
}
if (!maskAllText) {
// if the element is a button or anchor tag get the span text from any
// children and include it as/with the text property on the parent element
if (target.tagName.toLowerCase() === 'a' || target.tagName.toLowerCase() === 'button') {
elementsJson[0]['$el_text'] = getDirectAndNestedSpanText(target)
} else {
elementsJson[0]['$el_text'] = getSafeText(target)
}
}
let externalHref: string | undefined
if (href) {
elementsJson[0]['attr__href'] = href
const hrefHost = convertToURL(href)?.host
const locationHost = window?.location?.host
if (hrefHost && locationHost && hrefHost !== locationHost) {
externalHref = href
}
}
const props = extend(
getDefaultProperties(e.type),
// Sending "$elements" is deprecated. Only one client on US cloud uses this.
!elementsChainAsString ? { $elements: elementsJson } : {},
// Always send $elements_chain, as it's needed downstream in site app filtering
{ $elements_chain: getElementsChainString(elementsJson) },
elementsJson[0]?.['$el_text'] ? { $el_text: elementsJson[0]?.['$el_text'] } : {},
externalHref && e.type === 'click' ? { $external_click_url: externalHref } : {},
autocaptureAugmentProperties
)
return { props }
}
export class Autocapture {
instance: PostHog
_initialized: boolean = false
_isDisabledServerSide: boolean | null = null
_elementSelectors: Set<string> | null
rageclicks = new RageClick()
_elementsChainAsString = false
constructor(instance: PostHog) {
this.instance = instance
this._elementSelectors = null
}
private get _config(): AutocaptureConfig {
const config = isObject(this.instance.config.autocapture) ? this.instance.config.autocapture : {}
// precompile the regex
config.url_allowlist = config.url_allowlist?.map((url) => new RegExp(url))
config.url_ignorelist = config.url_ignorelist?.map((url) => new RegExp(url))
return config
}
_addDomEventHandlers(): void {
if (!this.isBrowserSupported()) {
logger.info('Disabling Automatic Event Collection because this browser is not supported')
return
}
if (!window || !document) {
return
}
const handler = (e: Event) => {
e = e || window?.event
try {
this._captureEvent(e)
} catch (error) {
logger.error('Failed to capture event', error)
}
}
addEventListener(document, 'submit', handler, { capture: true })
addEventListener(document, 'change', handler, { capture: true })
addEventListener(document, 'click', handler, { capture: true })
if (this._config.capture_copied_text) {
const copiedTextHandler = (e: Event) => {
e = e || window?.event
this._captureEvent(e, COPY_AUTOCAPTURE_EVENT)
}
addEventListener(document, 'copy', copiedTextHandler, { capture: true })
addEventListener(document, 'cut', copiedTextHandler, { capture: true })
}
}
public startIfEnabled() {
if (this.isEnabled && !this._initialized) {
this._addDomEventHandlers()
this._initialized = true
}
}
public onRemoteConfig(response: RemoteConfig) {
if (response.elementsChainAsString) {
this._elementsChainAsString = response.elementsChainAsString
}
if (this.instance.persistence) {
this.instance.persistence.register({
[AUTOCAPTURE_DISABLED_SERVER_SIDE]: !!response['autocapture_opt_out'],
})
}
// store this in-memory in case persistence is disabled
this._isDisabledServerSide = !!response['autocapture_opt_out']
this.startIfEnabled()
}
public setElementSelectors(selectors: Set<string>): void {
this._elementSelectors = selectors
}
public getElementSelectors(element: Element | null): string[] | null {
const elementSelectors: string[] = []
this._elementSelectors?.forEach((selector) => {
const matchedElements = document?.querySelectorAll(selector)
matchedElements?.forEach((matchedElement: Element) => {
if (element === matchedElement) {
elementSelectors.push(selector)
}
})
})
return elementSelectors
}
public get isEnabled(): boolean {
const persistedServerDisabled = this.instance.persistence?.props[AUTOCAPTURE_DISABLED_SERVER_SIDE]
const memoryDisabled = this._isDisabledServerSide
if (
isNull(memoryDisabled) &&
!isBoolean(persistedServerDisabled) &&
!this.instance.config.advanced_disable_decide
) {
// We only enable if we know that the server has not disabled it (unless decide is disabled)
return false
}
const disabledServer = this._isDisabledServerSide ?? !!persistedServerDisabled
const disabledClient = !this.instance.config.autocapture
return !disabledClient && !disabledServer
}
private _captureEvent(e: Event, eventName: EventName = '$autocapture'): boolean | void {
if (!this.isEnabled) {
return
}
/*** Don't mess with this code without running IE8 tests on it ***/
let target = getEventTarget(e)
if (isTextNode(target)) {
// defeat Safari bug (see: http://www.quirksmode.org/js/events_properties.html)
target = (target.parentNode || null) as Element | null
}
if (eventName === '$autocapture' && e.type === 'click' && e instanceof MouseEvent) {
if (
this.instance.config.rageclick &&
this.rageclicks?.isRageClick(e.clientX, e.clientY, new Date().getTime())
) {
this._captureEvent(e, '$rageclick')
}
}
const isCopyAutocapture = eventName === COPY_AUTOCAPTURE_EVENT
if (
target &&
shouldCaptureDomEvent(
target,
e,
this._config,
// mostly this method cares about the target element, but in the case of copy events,
// we want some of the work this check does without insisting on the target element's type
isCopyAutocapture,
// we also don't want to restrict copy checks to clicks,
// so we pass that knowledge in here, rather than add the logic inside the check
isCopyAutocapture ? ['copy', 'cut'] : undefined
)
) {
const { props, explicitNoCapture } = autocapturePropertiesForElement(target, {
e,
maskAllElementAttributes: this.instance.config.mask_all_element_attributes,
maskAllText: this.instance.config.mask_all_text,
elementAttributeIgnoreList: this._config.element_attribute_ignorelist,
elementsChainAsString: this._elementsChainAsString,
})
if (explicitNoCapture) {
return false
}
const elementSelectors = this.getElementSelectors(target)
if (elementSelectors && elementSelectors.length > 0) {
props['$element_selectors'] = elementSelectors
}
if (eventName === COPY_AUTOCAPTURE_EVENT) {
// you can't read the data from the clipboard event,
// but you can guess that you can read it from the window's current selection
const selectedContent = makeSafeText(window?.getSelection()?.toString())
const clipType = (e as ClipboardEvent).type || 'clipboard'
if (!selectedContent) {
return false
}
props['$selected_content'] = selectedContent
props['$copy_type'] = clipType
}
this.instance.capture(eventName, props)
return true
}
}
isBrowserSupported(): boolean {
return isFunction(document?.querySelectorAll)
}
}