|
| 1 | +// @ts-check |
| 2 | +import { test, expect } from "./fixtures.js"; |
| 3 | + |
| 4 | +test("Matomo tracking script is loaded", async ({ page, config }) => { |
| 5 | + await page.goto(config.baseURL); |
| 6 | + |
| 7 | + // Check if _paq object exists (Matomo tracking array) |
| 8 | + const hasPaq = await page.evaluate(() => typeof window._paq !== "undefined"); |
| 9 | + expect(hasPaq).toBeTruthy(); |
| 10 | +}); |
| 11 | + |
| 12 | +test("Matomo tracking requests are sent", async ({ page, config }) => { |
| 13 | + // Track all network requests |
| 14 | + const matomoRequests = []; |
| 15 | + |
| 16 | + page.on("request", (request) => { |
| 17 | + const url = request.url(); |
| 18 | + // Check for Matomo tracking requests (matomo.php or piwik.php) |
| 19 | + if (url.includes("matomo.php") || url.includes("piwik.php")) { |
| 20 | + matomoRequests.push({ |
| 21 | + url: url, |
| 22 | + method: request.method(), |
| 23 | + postData: request.postData(), |
| 24 | + }); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + // Navigate to the page |
| 29 | + await page.goto(config.baseURL); |
| 30 | + |
| 31 | + // Wait a bit for tracking to be sent |
| 32 | + await page.waitForTimeout(2000); |
| 33 | + |
| 34 | + // Verify that at least one Matomo tracking request was sent |
| 35 | + expect(matomoRequests.length).toBeGreaterThan(0); |
| 36 | + |
| 37 | + // Verify the tracking request contains expected parameters |
| 38 | + const firstRequest = matomoRequests[0]; |
| 39 | + expect(firstRequest.url).toMatch(/matomo\.php|piwik\.php/); |
| 40 | +}); |
| 41 | + |
| 42 | +test("Matomo tracking includes site ID", async ({ page, config }) => { |
| 43 | + // Track Matomo requests |
| 44 | + let matomoRequestUrl = null; |
| 45 | + |
| 46 | + page.on("request", (request) => { |
| 47 | + const url = request.url(); |
| 48 | + if (url.includes("matomo.php") || url.includes("piwik.php")) { |
| 49 | + matomoRequestUrl = url; |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + await page.goto(config.baseURL); |
| 54 | + await page.waitForTimeout(2000); |
| 55 | + |
| 56 | + expect(matomoRequestUrl).not.toBeNull(); |
| 57 | + |
| 58 | + // Check if the request contains idsite parameter |
| 59 | + expect(matomoRequestUrl).toMatch(/idsite=\d+/); |
| 60 | +}); |
| 61 | + |
| 62 | +test("Matomo tracking includes page URL", async ({ page, config }) => { |
| 63 | + let matomoRequestUrl = null; |
| 64 | + |
| 65 | + page.on("request", (request) => { |
| 66 | + const url = request.url(); |
| 67 | + if (url.includes("matomo.php") || url.includes("piwik.php")) { |
| 68 | + matomoRequestUrl = url; |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + await page.goto(config.baseURL); |
| 73 | + await page.waitForTimeout(2000); |
| 74 | + |
| 75 | + expect(matomoRequestUrl).not.toBeNull(); |
| 76 | + |
| 77 | + // Check if the request contains url parameter |
| 78 | + expect(matomoRequestUrl).toMatch(/url=/); |
| 79 | +}); |
| 80 | + |
| 81 | +test("Matomo tracking on blog page", async ({ page, config }) => { |
| 82 | + const matomoRequests = []; |
| 83 | + |
| 84 | + page.on("request", (request) => { |
| 85 | + const url = request.url(); |
| 86 | + if (url.includes("matomo.php") || url.includes("piwik.php")) { |
| 87 | + matomoRequests.push(url); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + // Navigate to a blog page |
| 92 | + await page.goto( |
| 93 | + config.baseURL + "/blog/2024-05-17-AI-support-was-added-to-QOwnNotes.html", |
| 94 | + ); |
| 95 | + await page.waitForTimeout(2000); |
| 96 | + |
| 97 | + // Verify tracking request was sent for blog page |
| 98 | + expect(matomoRequests.length).toBeGreaterThan(0); |
| 99 | + |
| 100 | + // Verify the URL in the tracking request contains the blog page path |
| 101 | + const trackingUrl = matomoRequests[0]; |
| 102 | + expect(trackingUrl).toMatch(/AI-support-was-added-to-QOwnNotes/); |
| 103 | +}); |
| 104 | + |
| 105 | +test("Matomo server responds successfully", async ({ page, config }) => { |
| 106 | + let matomoResponse = null; |
| 107 | + |
| 108 | + page.on("response", (response) => { |
| 109 | + const url = response.url(); |
| 110 | + if (url.includes("matomo.php") || url.includes("piwik.php")) { |
| 111 | + matomoResponse = response; |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + await page.goto(config.baseURL); |
| 116 | + await page.waitForTimeout(2000); |
| 117 | + |
| 118 | + expect(matomoResponse).not.toBeNull(); |
| 119 | + |
| 120 | + // Verify the Matomo server responds with a successful status |
| 121 | + expect(matomoResponse.status()).toBeLessThan(400); |
| 122 | +}); |
0 commit comments