From e11756edcdef1e3d634cf8f3a676211a6dd5b95b Mon Sep 17 00:00:00 2001 From: Xinyi Ye Date: Fri, 18 Oct 2024 10:48:29 -0700 Subject: [PATCH] fix(analytics-browser): should track file download when with url params --- .../src/plugins/file-download-tracking.ts | 2 +- .../test/plugins/file-download-tracking.test.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/analytics-browser/src/plugins/file-download-tracking.ts b/packages/analytics-browser/src/plugins/file-download-tracking.ts index bc5c01a49..a42a2b62d 100644 --- a/packages/analytics-browser/src/plugins/file-download-tracking.ts +++ b/packages/analytics-browser/src/plugins/file-download-tracking.ts @@ -78,7 +78,7 @@ export const fileDownloadTracking = (): EnrichmentPlugin => { }; const ext = - /\.(pdf|xlsx?|docx?|txt|rtf|csv|exe|key|pp(s|t|tx)|7z|pkg|rar|gz|zip|avi|mov|mp4|mpe?g|wmv|midi?|mp3|wav|wma)$/; + /\.(pdf|xlsx?|docx?|txt|rtf|csv|exe|key|pp(s|t|tx)|7z|pkg|rar|gz|zip|avi|mov|mp4|mpe?g|wmv|midi?|mp3|wav|wma)(\?.+)?$/; // Adds listener to existing anchor tags const links = Array.from(document.getElementsByTagName('a')); diff --git a/packages/analytics-browser/test/plugins/file-download-tracking.test.ts b/packages/analytics-browser/test/plugins/file-download-tracking.test.ts index e9d74e838..21cd0ddf5 100644 --- a/packages/analytics-browser/test/plugins/file-download-tracking.test.ts +++ b/packages/analytics-browser/test/plugins/file-download-tracking.test.ts @@ -36,9 +36,12 @@ describe('fileDownloadTracking', () => { expect(amplitude.track).toHaveBeenCalledTimes(0); }); - test('should track file_download event', async () => { + test.each([ + 'https://analytics.amplitude.com/files/my-file.pdf', + 'https://analytics.amplitude.com/files/my-file.pdf?foo=bar', + ])('should track file_download event', async (value) => { // setup - document.getElementById('my-link-id')?.setAttribute('href', 'https://analytics.amplitude.com/files/my-file.pdf'); + document.getElementById('my-link-id')?.setAttribute('href', value); const config = createConfigurationMock(); const plugin = fileDownloadTracking(); await plugin.setup?.(config, amplitude); @@ -54,7 +57,7 @@ describe('fileDownloadTracking', () => { [FILE_NAME]: '/files/my-file.pdf', [LINK_ID]: 'my-link-id', [LINK_TEXT]: 'my-link-text', - [LINK_URL]: 'https://analytics.amplitude.com/files/my-file.pdf', + [LINK_URL]: value, }); // stop observer and listeners