-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add gtag id only in prod * Create gtag.spec.ts * Base gtag tests on fixture to run in prod or other envs
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from '@playwright/test'; | ||
import config from '../../../config'; | ||
import { test } from '../../utils'; | ||
|
||
const googleTrackingId = config.gatsby.gaTrackingId; | ||
|
||
test.describe('Google Analytics Tracking', () => { | ||
test('Should track gtag only on production', async ({ page, runOnlyInProduction }) => { | ||
await page.goto('/'); | ||
|
||
const hasGoogleTracking = await page.locator(`script[src*="https://www.googletagmanager.com/gtag/js?id=${googleTrackingId}"]`).count(); | ||
|
||
expect(hasGoogleTracking).toBeGreaterThan(0); | ||
|
||
const [trackingRequest] = await Promise.all([ | ||
page.waitForRequest((request) => request.url().includes(`https://www.google-analytics.com`) && request.method() === 'POST'), | ||
|
||
]); | ||
|
||
expect(trackingRequest).toBeTruthy(); | ||
}); | ||
|
||
|
||
test('Should not track gtag outside production', async ({ page, runAnywhereExceptProduction }) => { | ||
await page.goto('/'); | ||
|
||
const hasGoogleTracking = await page.locator(`script[src*="https://www.googletagmanager.com/gtag/js?id=${googleTrackingId}"]`).count(); | ||
expect(hasGoogleTracking).toBe(0); | ||
|
||
let trackingRequestMade = false; | ||
page.on('request', (request) => { | ||
if (request.url().includes(`https://www.google-analytics.com`) && request.method() === 'POST') { | ||
trackingRequestMade = true; | ||
} | ||
}); | ||
|
||
await page.reload(); | ||
expect(trackingRequestMade).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters