Skip to content

Commit d730eb5

Browse files
test: e2e integration tests for ga and gtm (#189)
Co-authored-by: harlan <[email protected]>
1 parent 9285284 commit d730eb5

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

test/e2e/basic.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,34 @@ describe('basic', () => {
149149
expect(text).toMatchInlineSnapshot(`"/_scripts/6nd5bD9YCW.js"`)
150150
})
151151
})
152+
153+
describe('third-party-capital', () => {
154+
it('expect GA to collect data', {
155+
timeout: 10000,
156+
}, async () => {
157+
const page = await createPage('/tpc/ga')
158+
await page.waitForTimeout(500)
159+
160+
// wait for the collect request or timeout
161+
const request = page.waitForRequest(request => request.url().includes('google-analytics.com/g/collect'), {
162+
timeout: 10000,
163+
})
164+
await page.getByText('Trigger conversion').click()
165+
166+
await request
167+
})
168+
169+
it('expect GTM to work collect data', {
170+
timeout: 10000,
171+
}, async () => {
172+
const page = await createPage('/tpc/gtm')
173+
await page.waitForTimeout(500)
174+
175+
// wait for the collect request
176+
const request = page.waitForRequest(request => request.url().includes('analytics.google.com/g/collect?'), {
177+
timeout: 10000,
178+
})
179+
await page.getByText('trigger').click()
180+
await request
181+
})
182+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts" setup>
2+
import { useHead, useScriptGoogleAnalytics } from '#imports'
3+
4+
useHead({
5+
title: 'Google Analytics',
6+
})
7+
8+
// composables return the underlying api as a proxy object and a $script with the script state
9+
const { gtag, $script } = useScriptGoogleAnalytics({
10+
id: 'G-TR58L0EF8P',
11+
})
12+
function triggerConversion() {
13+
gtag('event', 'conversion')
14+
}
15+
</script>
16+
17+
<template>
18+
<div>
19+
<ClientOnly>
20+
<div>
21+
status: {{ $script.status.value }}
22+
</div>
23+
</ClientOnly>
24+
<button @click="triggerConversion">
25+
Trigger Conversion
26+
</button>
27+
</div>
28+
</template>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts" setup>
2+
import { useScriptGoogleTagManager } from '#imports'
3+
4+
const { dataLayer, $script } = useScriptGoogleTagManager({
5+
id: 'GTM-MNJD4B',
6+
})
7+
8+
function pushEvent() {
9+
dataLayer.push({
10+
event: 'page_view',
11+
page_title: 'GTM',
12+
})
13+
}
14+
</script>
15+
16+
<template>
17+
<div>
18+
<ClientOnly>
19+
<div>
20+
status: {{ $script.status }}
21+
</div>
22+
</ClientOnly>
23+
24+
<button @click="pushEvent">
25+
trigger
26+
</button>
27+
</div>
28+
</template>

0 commit comments

Comments
 (0)