File size: 5,168 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
/*
Tests for plausible-web.js script variant

Unlike in production, we're manually interpolating the script config in this file to
better test the script in isolation of the plausible codebase.
*/

import {
  expectPlausibleInAction,
  isEngagementEvent
} from './support/test-utils'
import { test, expect } from '@playwright/test'
import { LOCAL_SERVER_ADDR } from './support/server'
import {
  getConfiguredPlausibleWebSnippet,
  initializePageDynamically
} from './support/initialize-page-dynamically'

const DEFAULT_CONFIG = {
  domain: 'example.com',
  endpoint: `${LOCAL_SERVER_ADDR}/api/event`,
  captureOnLocalhost: true
}

test('with queue code from the web snippet, tracks `plausible` calls made before the script is loaded', async ({
  page
}, { testId }) => {
  const config = { ...DEFAULT_CONFIG }
  const { url } = await initializePageDynamically(page, {
    testId,
    scriptConfig: config,
    bodyContent:
      '<script>window.plausible("loaded", { props: { plausibleLoadedAtEventTime: window.plausible.l ? true : false }, interactive: false })</script>'
  })
  await expectPlausibleInAction(page, {
    action: () => page.goto(url),
    expectedRequests: [
      {
        n: 'loaded',
        p: { plausibleLoadedAtEventTime: false },
        i: false,
        d: config.domain,
        u: `${LOCAL_SERVER_ADDR}${url}`
      },
      { n: 'pageview', d: config.domain, u: `${LOCAL_SERVER_ADDR}${url}` }
    ]
  })
})

test('handles double-initialization of the script with a console.warn', async ({
  page
}, { testId }) => {
  const config = { ...DEFAULT_CONFIG, customProperties: { init: 1 } }
  const { url } = await initializePageDynamically(page, {
    testId,
    scriptConfig: config,
    bodyContent: /* HTML */ `<button onclick="window.plausible('Purchase')">
      Purchase
    </button>`
  })
  const messages: [string, string][] = []
  page.on('console', (message) => {
    messages.push([message.type(), message.text()])
  })

  await expectPlausibleInAction(page, {
    action: () => page.goto(url),
    expectedRequests: [{ n: 'pageview', p: { init: 1 } }],
    shouldIgnoreRequest: isEngagementEvent
  })

  await expect(
    page.evaluate(() =>
      // @ts-expect-error - window.plausible is defined
      window.plausible.init({
        captureOnLocalhost: true,
        customProperties: { init: 2 }
      })
    )
  ).resolves.toBeUndefined()

  expect(messages).toEqual([
    [
      'warning',
      'Plausible analytics script was already initialized, skipping init'
    ]
  ])

  await expectPlausibleInAction(page, {
    action: () => page.click('button'),
    expectedRequests: [{ n: 'Purchase', p: { init: 1 } }]
  })
})

test('if there are two snippets on the page, one wins, no warning is emitted', async ({
  page
}, { testId }) => {
  const config = { ...DEFAULT_CONFIG }
  const snippetAlfa = getConfiguredPlausibleWebSnippet({
    ...config,
    customProperties: { alfa: true }
  })
  const initCallAlfa =
    'plausible.init({"captureOnLocalhost":true,"customProperties":{"alfa":true}})'
  expect(snippetAlfa).toEqual(expect.stringContaining(initCallAlfa))
  const snippetBeta = getConfiguredPlausibleWebSnippet({
    ...config,
    customProperties: { beta: true }
  })
  const initCallBeta = `plausible.init({"captureOnLocalhost":true,"customProperties":{"beta":true}})`
  expect(snippetBeta).toEqual(expect.stringContaining(initCallBeta))

  const messages: [string, string][] = []
  page.on('console', (message) => {
    messages.push([message.type(), message.text()])
  })

  const { url } = await initializePageDynamically(page, {
    testId,
    scriptConfig: /* HTML */ `${snippetAlfa}${snippetBeta}`,
    bodyContent: ''
  })
  await expectPlausibleInAction(page, {
    action: () => page.goto(url),
    expectedRequests: [
      {
        n: 'pageview',
        d: config.domain,
        u: `${LOCAL_SERVER_ADDR}${url}`,
        p: { beta: true }
      }
    ],
    shouldIgnoreRequest: isEngagementEvent
  })
  expect(messages).toEqual([])
})

test('if domain is provided in `init`, it is ignored', async ({ page }, {
  testId
}) => {
  const config = { ...DEFAULT_CONFIG }
  const scriptConfig = getConfiguredPlausibleWebSnippet(config)
  const originalInitCall = 'plausible.init({"captureOnLocalhost":true})'
  // verify that the original snippet is what we expect it to be
  expect(scriptConfig).toEqual(expect.stringContaining(originalInitCall))
  const initCallWithDomainOverride = `plausible.init({"captureOnLocalhost":true,"domain":"sub.${config.domain}"})`
  const updatedScriptConfig = scriptConfig.replace(
    originalInitCall,
    initCallWithDomainOverride
  )
  // verify that the updated snippet has the domain override
  expect(updatedScriptConfig).toEqual(
    expect.stringContaining(initCallWithDomainOverride)
  )

  const { url } = await initializePageDynamically(page, {
    testId,
    scriptConfig: updatedScriptConfig,
    bodyContent: ''
  })
  await expectPlausibleInAction(page, {
    action: () => page.goto(url),
    expectedRequests: [
      { n: 'pageview', d: config.domain, u: `${LOCAL_SERVER_ADDR}${url}` }
    ],
    shouldIgnoreRequest: isEngagementEvent
  })
})