File size: 2,104 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
if (COMPILE_COMPAT) {
  var scriptEl = document.getElementById('plausible')
} else if (COMPILE_PLAUSIBLE_LEGACY_VARIANT) {
  // eslint-disable-next-line no-redeclare
  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) {
    // This will be dynamically replaced by a config json object in the script serving endpoint
    config = '<%= @config_js %>'
    Object.assign(config, options, {
      // Explicitly set domain after other options are applied as `plausible-web` does not support overriding it, except by transformRequest
      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 {
    // Legacy variant
    config.endpoint = scriptEl.getAttribute('data-api') || defaultEndpoint()
    config.domain = scriptEl.getAttribute('data-domain')
    config.logging = true
  }
}

export { config, scriptEl }