From 135471c32e1f4d306c629b24066f2fb5f14735cb Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Mon, 21 Nov 2022 16:17:44 +0200 Subject: [PATCH] Add tagged-events script extension (#2333) Adds a new script extension that allows tracking interactions with specific HTML elements on a website. For example - to track link clicks on one specific `` element, you can tag it like this: ```html ``` And you can also tag the link with custom property names and values: ```html ``` Tagging a link as above will send a custom event with the given name and props, if a `click` or `auxclick` browser event happens, and targets the link element. The tracking behavior is somewhat different based on the HTML element type: - `` - triggers on `click` and `auxclick` events - intercepts navigation based on the same rules as `outbound-links` and `file-downloads` - `
` - triggers on `submit` event - always intercepts navigation (calls `form.submit()` after preventing default and sending the Plausible event) - other (``, `
+ + + Span + + +
+ Div child +
+ + + + + +
+
+ + +
+
+ + + Link (without plausible-event-name class) + + + + + + Span (without plausible-event-name class) + + + + + +
+ +
div in a form
+ +
+ + + diff --git a/tracker/test/tagged-events.spec.js b/tracker/test/tagged-events.spec.js new file mode 100644 index 000000000..04bd8e5ad --- /dev/null +++ b/tracker/test/tagged-events.spec.js @@ -0,0 +1,109 @@ +const { test } = require('./support/harness') +const { mockRequest, mockManyRequests, isMac, expectCustomEvent } = require('./support/test-utils') +const { expect } = require('@playwright/test') + +test.describe('tagged-events extension', () => { + test('tracks a tagged link click with custom props + url prop', async ({ page }) => { + await page.goto('/tagged-event.html') + + const linkURL = await page.locator('#link').getAttribute('href') + + const plausibleRequestMock = mockRequest(page, '/api/event') + await page.click('#link') + expectCustomEvent(await plausibleRequestMock, 'Payment Complete', { amount: '100', method: "Credit Card", url: linkURL }) + }); + + test('tracks a tagged form submit with custom props when submitting by pressing enter', async ({ page }) => { + await page.goto('/tagged-event.html') + const plausibleRequestMock = mockRequest(page, '/api/event') + + const inputLocator = page.locator('#form-text-input') + await inputLocator.type('some input') + await inputLocator.press('Enter') + + expectCustomEvent(await plausibleRequestMock, 'Signup', { type: "Newsletter" }) + }); + + test('tracks submit on a form with a tagged parent when submit button is clicked', async ({ page }) => { + await page.goto('/tagged-event.html') + + const plausibleRequestMockList = mockManyRequests(page, '/api/event', 2) + + await page.click('#submit-form-with-tagged-parent') + + const requests = await plausibleRequestMockList + + expect(requests.length).toBe(1) + expectCustomEvent(requests[0], "Form Submit", {}) + }); + + test('tracks click and auxclick on any tagged HTML element', async ({ page }, workerInfo) => { + await page.goto('/tagged-event.html') + + const plausibleRequestMockList = mockManyRequests(page, '/api/event', 3) + + await page.click('#button') + await page.click('#span') + await page.click('#div', { modifiers: [isMac(workerInfo) ? 'Meta' : 'Control'] }) + + const requests = await plausibleRequestMockList + expect(requests.length).toBe(3) + requests.forEach(request => expectCustomEvent(request, 'Custom Event', { foo: "bar" })) + }); + + test('does not track elements without plausible-event-name class + link elements navigate', async ({ page }) => { + await page.goto('/tagged-event.html') + const linkLocator = page.locator('#not-tracked-link') + + const linkURL = await linkLocator.getAttribute('href') + + const plausibleRequestMock = mockRequest(page, '/api/event') + const navigationRequestMock = mockRequest(page, linkURL) + + await page.click('#not-tracked-button') + await page.click('#not-tracked-span') + await linkLocator.click() + + expect(await plausibleRequestMock, "should not have made Plausible request").toBeNull() + expect((await navigationRequestMock).url()).toContain(linkURL) + }); + + test('tracks tagged HTML elements when their child element is clicked', async ({ page }, workerInfo) => { + await page.goto('/tagged-event.html') + + const plausibleRequestMockList = mockManyRequests(page, '/api/event', 2) + + await page.click('#h2-with-link-parent', { modifiers: [isMac(workerInfo) ? 'Meta' : 'Control'] }) + await page.click('#link-with-div-parent') + + const requests = await plausibleRequestMockList + expect(requests.length).toBe(2) + requests.forEach(request => expectCustomEvent(request, 'Custom Event', { foo: "bar" })) + }); + + test('tracks tagged element that is dynamically added to the DOM', async ({ page }) => { + await page.goto('/tagged-event.html') + + const plausibleRequestMock = mockRequest(page, '/api/event') + + const buttonLocator = page.locator('#dynamic-tagged-button') + await buttonLocator.waitFor({state: 'visible'}) + await page.waitForTimeout(500) + + await buttonLocator.click() + + expectCustomEvent(await plausibleRequestMock, 'Custom Event', {}) + }); + + test('does not track clicks inside a tagged form, except submit click', async ({ page }) => { + await page.goto('/tagged-event.html') + + const plausibleRequestMock = mockRequest(page, '/api/event') + + await page.click('#form') + await page.click('#form-input') + await page.click('#form-div') + + expect(await plausibleRequestMock, "should not have made Plausible request").toBeNull() + }); +});