Skip to content

Commit

Permalink
test: e2e integration tests for ga and gtm (#189)
Browse files Browse the repository at this point in the history
Co-authored-by: harlan <[email protected]>
  • Loading branch information
huang-julien and harlan-zw committed Aug 5, 2024
1 parent 9285284 commit d730eb5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,34 @@ describe('basic', () => {
expect(text).toMatchInlineSnapshot(`"/_scripts/6nd5bD9YCW.js"`)
})
})

describe('third-party-capital', () => {
it('expect GA to collect data', {
timeout: 10000,
}, async () => {
const page = await createPage('/tpc/ga')
await page.waitForTimeout(500)

// wait for the collect request or timeout
const request = page.waitForRequest(request => request.url().includes('google-analytics.com/g/collect'), {
timeout: 10000,
})
await page.getByText('Trigger conversion').click()

await request
})

it('expect GTM to work collect data', {
timeout: 10000,
}, async () => {
const page = await createPage('/tpc/gtm')
await page.waitForTimeout(500)

// wait for the collect request
const request = page.waitForRequest(request => request.url().includes('analytics.google.com/g/collect?'), {
timeout: 10000,
})
await page.getByText('trigger').click()
await request
})
})
28 changes: 28 additions & 0 deletions test/fixtures/basic/pages/tpc/ga.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="ts" setup>
import { useHead, useScriptGoogleAnalytics } from '#imports'
useHead({
title: 'Google Analytics',
})
// composables return the underlying api as a proxy object and a $script with the script state
const { gtag, $script } = useScriptGoogleAnalytics({
id: 'G-TR58L0EF8P',
})
function triggerConversion() {
gtag('event', 'conversion')
}
</script>

<template>
<div>
<ClientOnly>
<div>
status: {{ $script.status.value }}
</div>
</ClientOnly>
<button @click="triggerConversion">
Trigger Conversion
</button>
</div>
</template>
28 changes: 28 additions & 0 deletions test/fixtures/basic/pages/tpc/gtm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="ts" setup>
import { useScriptGoogleTagManager } from '#imports'
const { dataLayer, $script } = useScriptGoogleTagManager({
id: 'GTM-MNJD4B',
})
function pushEvent() {
dataLayer.push({
event: 'page_view',
page_title: 'GTM',
})
}
</script>

<template>
<div>
<ClientOnly>
<div>
status: {{ $script.status }}
</div>
</ClientOnly>

<button @click="pushEvent">
trigger
</button>
</div>
</template>

0 comments on commit d730eb5

Please sign in to comment.