File size: 4,571 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 | import { sendRequest } from './networking'
import {
prePageviewTrack,
postPageviewTrack,
onPageviewIgnored
} from './engagement'
import { config, scriptEl } from './config'
export function track(eventName, options) {
if (COMPILE_PLAUSIBLE_NPM && !config.isInitialized) {
throw new Error(
'plausible.track() can only be called after plausible.init()'
)
}
var isPageview = eventName === 'pageview'
if (isPageview) {
prePageviewTrack()
}
if (!(COMPILE_LOCAL && (!COMPILE_CONFIG || config.captureOnLocalhost))) {
if (
/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(
location.hostname
) ||
location.protocol === 'file:'
) {
return onIgnoredEvent(eventName, options, 'localhost')
}
if (
(window._phantom ||
window.__nightmare ||
window.navigator.webdriver ||
window.Cypress) &&
!window.__plausible
) {
return onIgnoredEvent(eventName, options)
}
}
try {
if (window.localStorage.plausible_ignore === 'true') {
return onIgnoredEvent(eventName, options, 'localStorage flag')
}
} catch (_error) {
// ignore error
}
if (COMPILE_EXCLUSIONS) {
var dataIncludeAttr = scriptEl && scriptEl.getAttribute('data-include')
var dataExcludeAttr = scriptEl && scriptEl.getAttribute('data-exclude')
function pathMatches(wildcardPath) {
var actualPath = location.pathname
if (COMPILE_HASH && (!COMPILE_CONFIG || config.hashBasedRouting)) {
actualPath += location.hash
}
return actualPath.match(
new RegExp(
'^' +
wildcardPath
.trim()
.replace(/\*\*/g, '.*')
// eslint-disable-next-line no-useless-escape
.replace(/([^\.])\*/g, '$1[^\\s\/]*') +
// eslint-disable-next-line no-useless-escape
'\/?$'
)
)
}
if (isPageview) {
var isIncluded =
!dataIncludeAttr ||
(dataIncludeAttr && dataIncludeAttr.split(',').some(pathMatches))
var isExcluded =
dataExcludeAttr && dataExcludeAttr.split(',').some(pathMatches)
if (!isIncluded || isExcluded)
return onIgnoredEvent(eventName, options, 'exclusion rule')
}
}
var payload = {}
payload.n = eventName
payload.v = COMPILE_TRACKER_SCRIPT_VERSION
if (COMPILE_MANUAL) {
var customURL = options && (options.u || options.url)
payload.u = customURL ? customURL : location.href
} else {
payload.u = location.href
}
payload.d = config.domain
payload.r = document.referrer || null
if (COMPILE_PLAUSIBLE_LEGACY_VARIANT && options && options.meta) {
payload.m = JSON.stringify(options.meta)
}
if (options && options.props) {
payload.p = options.props
}
if (options && options.interactive === false) {
payload.i = false
}
if (COMPILE_REVENUE) {
if (options && options.revenue) {
payload.$ = options.revenue
}
}
if (COMPILE_PAGEVIEW_PROPS) {
var propAttributes = scriptEl.getAttributeNames().filter(function (name) {
return name.substring(0, 6) === 'event-'
})
var props = payload.p || {}
propAttributes.forEach(function (attribute) {
var propKey = attribute.replace('event-', '')
var propValue = scriptEl.getAttribute(attribute)
props[propKey] = props[propKey] || propValue
})
payload.p = props
}
// Track custom properties for pageviews and other events
if (COMPILE_CUSTOM_PROPERTIES && config.customProperties) {
// eslint-disable-next-line no-redeclare
var props = config.customProperties
if (typeof props === 'function') {
props = config.customProperties(eventName)
}
if (typeof props === 'object') {
payload.p = Object.assign({}, props, payload.p)
}
}
if (COMPILE_HASH && (!COMPILE_CONFIG || config.hashBasedRouting)) {
payload.h = 1
}
if (
(COMPILE_PLAUSIBLE_WEB || COMPILE_PLAUSIBLE_NPM) &&
typeof config.transformRequest === 'function'
) {
payload = config.transformRequest(payload)
if (!payload) {
return onIgnoredEvent(eventName, options, 'transformRequest')
}
}
if (isPageview) {
postPageviewTrack(payload)
}
sendRequest(config.endpoint, payload, options)
}
function onIgnoredEvent(eventName, options, reason) {
if (reason && config.logging) {
console.warn('Ignoring Event: ' + reason)
}
options && options.callback && options.callback()
if (eventName === 'pageview') {
onPageviewIgnored()
}
}
|