File size: 9,980 Bytes
6778ee0 | 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 | // This file has the logic for tracking tagged events, form submissions, file downloads and outbound links.
import { config, scriptEl } from './config'
import { track } from './track'
export var DEFAULT_FILE_TYPES = [
'pdf',
'xlsx',
'docx',
'txt',
'rtf',
'csv',
'exe',
'key',
'pps',
'ppt',
'pptx',
'7z',
'pkg',
'rar',
'gz',
'zip',
'avi',
'mov',
'mp4',
'mpeg',
'wmv',
'midi',
'mp3',
'wav',
'wma',
'dmg'
]
var MIDDLE_MOUSE_BUTTON = 1
var PARENTS_TO_SEARCH_LIMIT = 3
var fileTypesToTrack = DEFAULT_FILE_TYPES
function getLinkEl(link) {
while (
link &&
(typeof link.tagName === 'undefined' || !isLink(link) || !link.href)
) {
link = link.parentNode
}
return link
}
function isLink(element) {
return element && element.tagName && element.tagName.toLowerCase() === 'a'
}
function shouldInterceptNavigation(event, link) {
// If default has been prevented by an external script, Plausible should not intercept navigation.
if (event.defaultPrevented) return false
var target = link.target
// If the link directs to open the link in a different context, or we're not sure, do not intercept navigation
if (
target &&
(typeof target !== 'string' || !target.match(/^_(self|parent|top)$/i))
)
return false
// If the click is not a regular click (e.g. ctrl, meta, shift, or not a click event), do not intercept navigation
if (
event.ctrlKey ||
event.metaKey ||
event.shiftKey ||
event.type !== 'click'
)
return false
return true
}
function handleLinkClickEvent(event) {
if (event.type === 'auxclick' && event.button !== MIDDLE_MOUSE_BUTTON) {
return
}
var link = getLinkEl(event.target)
var hrefWithoutQuery =
link && typeof link.href === 'string' && link.href.split('?')[0]
if (COMPILE_TAGGED_EVENTS) {
if (isElementOrParentTagged(link, 0)) {
// Return to prevent sending multiple events with the same action.
// Clicks on tagged links are handled by another function.
return
}
}
if (COMPILE_OUTBOUND_LINKS && (!COMPILE_CONFIG || config.outboundLinks)) {
if (isOutboundLink(link)) {
return sendLinkClickEvent(event, link, {
name: 'Outbound Link: Click',
props: { url: link.href }
})
}
}
if (COMPILE_FILE_DOWNLOADS && (!COMPILE_CONFIG || config.fileDownloads)) {
if (isDownloadToTrack(hrefWithoutQuery)) {
return sendLinkClickEvent(event, link, {
name: 'File Download',
props: { url: hrefWithoutQuery }
})
}
}
}
function sendLinkClickEvent(event, link, eventAttrs) {
// In some legacy variants, this block delays opening the link up to 5 seconds,
// or until analytics request finishes, otherwise navigation could prevent the analytics event from being sent.
var attrs
if (COMPILE_COMPAT) {
var followedLink = false
function followLink() {
if (!followedLink) {
followedLink = true
window.location = link.href
}
}
if (shouldInterceptNavigation(event, link)) {
attrs = { props: eventAttrs.props, callback: followLink }
if (COMPILE_REVENUE) {
attrs.revenue = eventAttrs.revenue
}
track(eventAttrs.name, attrs)
setTimeout(followLink, 5000)
event.preventDefault()
} else {
attrs = { props: eventAttrs.props }
if (COMPILE_REVENUE) {
attrs.revenue = eventAttrs.revenue
}
track(eventAttrs.name, attrs)
}
} else {
attrs = { props: eventAttrs.props }
if (COMPILE_REVENUE) {
attrs.revenue = eventAttrs.revenue
}
track(eventAttrs.name, attrs)
}
}
function isOutboundLink(link) {
return (
link &&
typeof link.href === 'string' &&
link.host &&
link.host !== location.host
)
}
function isDownloadToTrack(url) {
if (!url) {
return false
}
var fileType = url.split('.').pop()
return fileTypesToTrack.some(function (fileTypeToTrack) {
return fileTypeToTrack === fileType
})
}
function isTagged(element) {
var classList = element && element.classList
if (classList) {
for (var i = 0; i < classList.length; i++) {
if (classList.item(i).match(/plausible-event-name(=|--)(.+)/)) {
return true
}
}
}
return false
}
function isElementOrParentTagged(element, parentsChecked) {
if (!element || parentsChecked > PARENTS_TO_SEARCH_LIMIT) {
return false
}
if (isTagged(element)) {
return true
}
return isElementOrParentTagged(element.parentNode, parentsChecked + 1)
}
// Finds event attributes by iterating over the given element's (or its
// parent's) classList. Returns an object with `name` and `props` keys.
function getTaggedEventAttributes(htmlElement) {
var taggedElement = isTagged(htmlElement)
? htmlElement
: htmlElement && htmlElement.parentNode
var eventAttrs = { name: null, props: {} }
if (COMPILE_REVENUE) {
eventAttrs.revenue = {}
}
var classList = taggedElement && taggedElement.classList
if (!classList) {
return eventAttrs
}
for (var i = 0; i < classList.length; i++) {
var className = classList.item(i)
var key
var value
var matchList = className.match(/plausible-event-(.+)(=|--)(.+)/)
if (matchList) {
key = matchList[1]
value = matchList[3].replace(/\+/g, ' ')
if (key.toLowerCase() == 'name') {
eventAttrs.name = value
} else {
eventAttrs.props[key] = value
}
}
if (COMPILE_REVENUE) {
var revenueMatchList = className.match(/plausible-revenue-(.+)(=|--)(.+)/)
if (revenueMatchList) {
key = revenueMatchList[1]
value = revenueMatchList[3]
eventAttrs.revenue[key] = value
}
}
}
return eventAttrs
}
export function init() {
document.addEventListener('click', handleLinkClickEvent)
document.addEventListener('auxclick', handleLinkClickEvent)
if (COMPILE_FILE_DOWNLOADS && (!COMPILE_CONFIG || config.fileDownloads)) {
if (COMPILE_CONFIG) {
if (
typeof config.fileDownloads === 'object' &&
Array.isArray(config.fileDownloads.fileExtensions)
) {
fileTypesToTrack = config.fileDownloads.fileExtensions
}
} else {
var fileTypesAttr = scriptEl.getAttribute('file-types')
var addFileTypesAttr = scriptEl.getAttribute('add-file-types')
if (fileTypesAttr) {
fileTypesToTrack = fileTypesAttr.split(',')
}
if (addFileTypesAttr) {
fileTypesToTrack = addFileTypesAttr
.split(',')
.concat(DEFAULT_FILE_TYPES)
}
}
}
if (COMPILE_CONFIG && config.formSubmissions) {
function trackFormSubmission(e) {
if (e.target.hasAttribute('novalidate') || e.target.checkValidity()) {
if (COMPILE_TAGGED_EVENTS && isElementOrParentTagged(e.target, 0)) {
// If the form is tagged, we don't track it as a generic form submission.
return
}
track('Form: Submission')
}
}
document.addEventListener('submit', trackFormSubmission, true)
}
if (COMPILE_TAGGED_EVENTS) {
function handleTaggedFormSubmitEvent(event) {
var form = event.target
var eventAttrs = getTaggedEventAttributes(form)
if (!eventAttrs.name) {
return
}
var attrs
// In some legacy variants, this block delays submitting the form for up to 5 seconds,
// or until analytics request finishes, otherwise form-related navigation could prevent the analytics event from being sent.
if (COMPILE_COMPAT) {
event.preventDefault()
var formSubmitted = false
function submitForm() {
if (!formSubmitted) {
formSubmitted = true
form.submit()
}
}
setTimeout(submitForm, 5000)
attrs = { props: eventAttrs.props, callback: submitForm }
if (COMPILE_REVENUE) {
attrs.revenue = eventAttrs.revenue
}
track(eventAttrs.name, attrs)
} else {
attrs = { props: eventAttrs.props }
if (COMPILE_REVENUE) {
attrs.revenue = eventAttrs.revenue
}
track(eventAttrs.name, attrs)
}
}
function isForm(element) {
return (
element && element.tagName && element.tagName.toLowerCase() === 'form'
)
}
function handleTaggedElementClickEvent(event) {
if (event.type === 'auxclick' && event.button !== MIDDLE_MOUSE_BUTTON) {
return
}
var clicked = event.target
var clickedLink
var taggedElement
// Iterate over parents to find the tagged element. Also search for
// a link element to call for different tracking behavior if found.
for (var i = 0; i <= PARENTS_TO_SEARCH_LIMIT; i++) {
if (!clicked) {
break
}
// Clicks inside forms are not tracked. Only form submits are.
if (isForm(clicked)) {
return
}
if (isLink(clicked)) {
clickedLink = clicked
}
if (isTagged(clicked)) {
taggedElement = clicked
}
clicked = clicked.parentNode
}
if (taggedElement) {
var eventAttrs = getTaggedEventAttributes(taggedElement)
if (clickedLink) {
// if the clicked tagged element is a link, we attach the `url` property
// automatically for user convenience
eventAttrs.props.url = clickedLink.href
sendLinkClickEvent(event, clickedLink, eventAttrs)
} else {
var attrs = {}
attrs.props = eventAttrs.props
if (COMPILE_REVENUE) {
attrs.revenue = eventAttrs.revenue
}
track(eventAttrs.name, attrs)
}
}
}
document.addEventListener('submit', handleTaggedFormSubmitEvent)
document.addEventListener('click', handleTaggedElementClickEvent)
document.addEventListener('auxclick', handleTaggedElementClickEvent)
}
}
|