| if (COMPILE_COMPAT) { |
| var scriptEl = document.getElementById('plausible') |
| } else if (COMPILE_PLAUSIBLE_LEGACY_VARIANT) { |
| |
| var scriptEl = document.currentScript |
| } |
|
|
| var config = {} |
|
|
| function defaultEndpoint() { |
| if (COMPILE_COMPAT) { |
| var pathArray = scriptEl.src.split('/') |
| var protocol = pathArray[0] |
| var host = pathArray[2] |
| return protocol + '//' + host + '/api/event' |
| } else { |
| return new URL(scriptEl.src).origin + '/api/event' |
| } |
| } |
|
|
| export function getOptionsWithDefaults(initOptions) { |
| if (COMPILE_PLAUSIBLE_WEB) { |
| return Object.assign(initOptions, { |
| autoCapturePageviews: initOptions.autoCapturePageviews !== false, |
| logging: initOptions.logging !== false, |
| lib: initOptions.lib || 'web' |
| }) |
| } |
| if (COMPILE_PLAUSIBLE_NPM) { |
| return Object.assign(initOptions, { |
| autoCapturePageviews: initOptions.autoCapturePageviews !== false, |
| logging: initOptions.logging !== false, |
| bindToWindow: initOptions.bindToWindow !== false |
| }) |
| } |
| } |
|
|
| export function init(options) { |
| if (COMPILE_PLAUSIBLE_WEB) { |
| |
| config = '<%= @config_js %>' |
| Object.assign(config, options, { |
| |
| domain: config.domain |
| }) |
| } else if (COMPILE_PLAUSIBLE_NPM) { |
| if (config.isInitialized) { |
| throw new Error('plausible.init() can only be called once') |
| } |
| if (!options || !options.domain) { |
| throw new Error('plausible.init(): domain argument is required') |
| } |
| if (!options.endpoint) { |
| options.endpoint = 'https://plausible.io/api/event' |
| } |
| Object.assign(config, options) |
| config.isInitialized = true |
| } else { |
| |
| config.endpoint = scriptEl.getAttribute('data-api') || defaultEndpoint() |
| config.domain = scriptEl.getAttribute('data-domain') |
| config.logging = true |
| } |
| } |
|
|
| export { config, scriptEl } |
|
|