-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Script v2: Update tests relating to tagged a elements as well as examples from docs #5687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| src/p.js | ||
| src/plausible.js | ||
| npm_package/plausible.js | ||
| node_modules/ |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,12 +6,14 @@ import { | |
| e, | ||
| expectPlausibleInAction, | ||
| hideAndShowCurrentTab, | ||
| isPageviewEvent, | ||
| isEngagementEvent, | ||
| switchByMode | ||
| } from './support/test-utils' | ||
| import { test } from '@playwright/test' | ||
| import { test, expect } from '@playwright/test' | ||
| import { ScriptConfig } from './support/types' | ||
| import { LOCAL_SERVER_ADDR } from './support/server' | ||
|
|
||
| const DEFAULT_CONFIG: ScriptConfig = { | ||
| domain: 'example.com', | ||
| endpoint: `${LOCAL_SERVER_ADDR}/api/event`, | ||
|
|
@@ -263,3 +265,178 @@ for (const mode of ['web', 'esm']) { | |
| }) | ||
| }) | ||
| } | ||
|
|
||
| test.describe(`transformRequest examples from /docs work`, () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should help verify that what we recommend to users actually works as intended |
||
| test.beforeEach(async ({ page }) => { | ||
| await page | ||
| .context() | ||
| .route(new RegExp('(http|https)://example\\.com.*'), async (route) => { | ||
| await route.fulfill({ | ||
| status: 200, | ||
| contentType: 'text/html', | ||
| body: /* HTML */ `<!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>mocked page</title> | ||
| </head> | ||
| <body> | ||
| mocked page | ||
| </body> | ||
| </html>` | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('you can omit automatically tracked url property from tagged link clicks', async ({ | ||
| page | ||
| }, { testId }) => { | ||
| function omitAutomaticUrlProperty(payload) { | ||
| if (payload.p && payload.p.url) { | ||
| delete payload.p.url | ||
| } | ||
| return payload | ||
| } | ||
| const config = { | ||
| ...DEFAULT_CONFIG, | ||
| transformRequest: omitAutomaticUrlProperty | ||
| } | ||
| const { url } = await initializePageDynamically(page, { | ||
| testId, | ||
| scriptConfig: config, | ||
| bodyContent: /* HTML */ `<a | ||
| class="plausible-event-name=Purchase plausible-event-discounted=true" | ||
| href="https://example.com/target?user=sensitive" | ||
| >Purchase</a | ||
| >` | ||
| }) | ||
|
|
||
| await expectPlausibleInAction(page, { | ||
| action: async () => { | ||
| await page.goto(url) | ||
| await page.click('a') | ||
| }, | ||
| expectedRequests: [ | ||
| { | ||
| n: 'Purchase', | ||
| p: { discounted: 'true' } // <-- no url property | ||
| } | ||
| ], | ||
| shouldIgnoreRequest: [isPageviewEvent, isEngagementEvent] | ||
| }) | ||
| await expect(page.getByText('mocked page')).toBeVisible() | ||
| }) | ||
|
|
||
| for (const { hashBasedRouting, urlSuffix, expectedUrlSuffix } of [ | ||
| { | ||
| hashBasedRouting: true, | ||
| urlSuffix: | ||
| '?utm_source=example&utm_medium=referral&utm_campaign=test#fragment', | ||
| expectedUrlSuffix: '#fragment' | ||
| }, | ||
| { | ||
| hashBasedRouting: false, | ||
| urlSuffix: '?utm_source=example&utm_medium=referral&utm_campaign=test', | ||
| expectedUrlSuffix: '' | ||
| } | ||
| ]) { | ||
| test(`you can omit UTM properties from pageview urls (hashBasedRouting: ${hashBasedRouting})`, async ({ | ||
| page | ||
| }, { testId }) => { | ||
| function omitUTMProperties(payload) { | ||
| const parts = payload.u.split('?') | ||
| let urlWithoutQuery = parts.shift() | ||
|
|
||
| if (payload.h) { | ||
| const fragment = parts.join('?').split('#')[1] | ||
| urlWithoutQuery = | ||
| typeof fragment === 'string' | ||
| ? urlWithoutQuery + '#' + fragment | ||
| : urlWithoutQuery | ||
| } | ||
|
|
||
| payload.u = urlWithoutQuery | ||
| return payload | ||
| } | ||
|
|
||
| const config = { | ||
| ...DEFAULT_CONFIG, | ||
| hashBasedRouting, | ||
| transformRequest: omitUTMProperties | ||
| } | ||
|
|
||
| // the star path is needed for the dynamic page to load when accessing it with query params | ||
| const path = '*' | ||
| const { url } = await initializePageDynamically(page, { | ||
| testId, | ||
| path, | ||
| scriptConfig: config, | ||
| bodyContent: '' | ||
| }) | ||
|
|
||
| const [actualUrl] = url.split('*') | ||
|
|
||
| await expectPlausibleInAction(page, { | ||
| action: async () => { | ||
| await page.goto(`${actualUrl}${urlSuffix}`) | ||
| // await page.click('a') | ||
| }, | ||
| expectedRequests: [ | ||
| { | ||
| n: 'pageview', | ||
| u: `${LOCAL_SERVER_ADDR}${actualUrl}${expectedUrlSuffix}` | ||
| } | ||
| ], | ||
| shouldIgnoreRequest: [isEngagementEvent] | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| test('you can track pages using their canonical url', async ({ page }, { | ||
| testId | ||
| }) => { | ||
| function rewriteUrlToCanonicalUrl(payload) { | ||
| // Get the canonical URL element | ||
| const canonicalMeta = document.querySelector('link[rel="canonical"]') | ||
| // Use the canonical URL if it exists, falling back on the regular URL when it doesn't. | ||
| if (canonicalMeta) { | ||
| // @ts-expect-error - canonicalMeta definitely has the href attribute | ||
| payload.u = canonicalMeta.href + window.location.search | ||
| } | ||
| return payload | ||
| } | ||
|
|
||
| // the star path is needed for the dynamic page to load when accessing it with query params | ||
| const nonCanonicalPath = '/products/clothes/shoes/banana-leather-shoe*' | ||
| const { url } = await initializePageDynamically(page, { | ||
| testId, | ||
| path: nonCanonicalPath, | ||
| scriptConfig: /* HTML */ ` | ||
| <link rel="canonical" href="/products/banana-leather-shoe" /> | ||
| <script type="module"> | ||
| import { init, track } from '/tracker/js/npm_package/plausible.js' | ||
| init( | ||
| ${serializeWithFunctions({ | ||
| ...DEFAULT_CONFIG, | ||
| transformRequest: rewriteUrlToCanonicalUrl | ||
| })} | ||
| ) | ||
| </script> | ||
| `, | ||
| bodyContent: '' | ||
| }) | ||
| const [actualUrl] = url.split('*') | ||
|
|
||
| await expectPlausibleInAction(page, { | ||
| action: async () => { | ||
| await page.goto(`${actualUrl}?utm_source=example`) | ||
| }, | ||
| expectedRequests: [ | ||
| { | ||
| n: 'pageview', | ||
| u: `${LOCAL_SERVER_ADDR}/products/banana-leather-shoe?utm_source=example` | ||
| } | ||
| ], | ||
| shouldIgnoreRequest: [isEngagementEvent] | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.