|
| 1 | +import { DUB_ANALYTICS_SCRIPT_URL } from '@/app/constants'; |
| 2 | +import { test, expect } from '@playwright/test'; |
| 3 | + |
| 4 | +declare global { |
| 5 | + interface Window { |
| 6 | + _dubAnalytics: any; |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +test.describe('Outbound domains tracking', () => { |
| 11 | + test('should add tracking parameters to outbound links', async ({ page }) => { |
| 12 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 13 | + |
| 14 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 15 | + |
| 16 | + await page.waitForTimeout(1000); |
| 17 | + |
| 18 | + // Check that outbound links have tracking parameters |
| 19 | + const exampleLink = await page.$('a[href*="example.com"]'); |
| 20 | + const otherLink = await page.$('a[href*="other.com"]'); |
| 21 | + const unrelatedLink = await page.$('a[href*="unrelated.com"]'); |
| 22 | + |
| 23 | + const exampleHref = await exampleLink?.getAttribute('href'); |
| 24 | + const otherHref = await otherLink?.getAttribute('href'); |
| 25 | + const unrelatedHref = await unrelatedLink?.getAttribute('href'); |
| 26 | + |
| 27 | + expect(exampleHref).toContain('dub_id=test-click-id'); |
| 28 | + expect(otherHref).toContain('dub_id=test-click-id'); |
| 29 | + expect(unrelatedHref).not.toContain('dub_id=test-click-id'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('should handle iframe src attributes', async ({ page }) => { |
| 33 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 34 | + |
| 35 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 36 | + |
| 37 | + await page.waitForTimeout(2000); |
| 38 | + |
| 39 | + const iframe = await page.$('iframe'); |
| 40 | + const iframeSrc = await iframe?.getAttribute('src'); |
| 41 | + expect(iframeSrc).toContain('dub_id=test-click-id'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('should not add tracking to links on the same domain', async ({ |
| 45 | + page, |
| 46 | + }) => { |
| 47 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 48 | + |
| 49 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 50 | + |
| 51 | + await page.waitForTimeout(2000); |
| 52 | + |
| 53 | + const internalLink = await page.$('a[href*="getacme.link"]'); |
| 54 | + const externalLink = await page.$('a[href*="example.com"]'); |
| 55 | + |
| 56 | + const internalHref = await internalLink?.getAttribute('href'); |
| 57 | + const externalHref = await externalLink?.getAttribute('href'); |
| 58 | + |
| 59 | + expect(internalHref).not.toContain('dub_id=test-click-id'); |
| 60 | + expect(externalHref).toContain('dub_id=test-click-id'); |
| 61 | + }); |
| 62 | + |
| 63 | + // TODO: Fix this test |
| 64 | + test.skip('should handle dynamically added links', async ({ page }) => { |
| 65 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 66 | + |
| 67 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 68 | + |
| 69 | + // Add a link dynamically |
| 70 | + await page.evaluate(() => { |
| 71 | + const container = document.getElementById('container'); |
| 72 | + const link = document.createElement('a'); |
| 73 | + link.href = 'https://dynamic-link.com'; |
| 74 | + link.textContent = 'Dynamic Link'; |
| 75 | + container?.appendChild(link); |
| 76 | + }); |
| 77 | + |
| 78 | + await page.waitForTimeout(2500); |
| 79 | + |
| 80 | + const dynamicLink = await page.$('a[href*="dynamic-link.com"]'); |
| 81 | + const dynamicHref = await dynamicLink?.getAttribute('href'); |
| 82 | + expect(dynamicHref).toContain('dub_id=test-click-id'); |
| 83 | + }); |
| 84 | + |
| 85 | + test('should handle SPA navigation', async ({ page }) => { |
| 86 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 87 | + |
| 88 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 89 | + |
| 90 | + // Simulate SPA navigation |
| 91 | + await page.evaluate(() => { |
| 92 | + history.pushState({}, '', '/new-page'); |
| 93 | + const container = document.getElementById('container'); |
| 94 | + const link = document.createElement('a'); |
| 95 | + link.href = 'https://example.com'; |
| 96 | + link.textContent = 'SPA Link'; |
| 97 | + container?.appendChild(link); |
| 98 | + }); |
| 99 | + |
| 100 | + await page.waitForTimeout(2500); |
| 101 | + |
| 102 | + const spaLink = await page.$('a[href*="example.com"]'); |
| 103 | + const spaHref = await spaLink?.getAttribute('href'); |
| 104 | + expect(spaHref).toContain('dub_id=test-click-id'); |
| 105 | + }); |
| 106 | + |
| 107 | + test('should handle www. prefix and subdomains correctly', async ({ |
| 108 | + page, |
| 109 | + }) => { |
| 110 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 111 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 112 | + |
| 113 | + await page.waitForTimeout(2500); |
| 114 | + |
| 115 | + // Check www. prefix handling |
| 116 | + const wwwLink = await page.$('a[href*="www.example.com"]'); |
| 117 | + const noWwwLink = await page.$( |
| 118 | + 'a[href*="example.com"]:not([href*="www."])', |
| 119 | + ); |
| 120 | + const wwwHref = await wwwLink?.getAttribute('href'); |
| 121 | + const noWwwHref = await noWwwLink?.getAttribute('href'); |
| 122 | + |
| 123 | + expect(wwwHref).toContain('dub_id=test-click-id'); |
| 124 | + expect(noWwwHref).toContain('dub_id=test-click-id'); |
| 125 | + |
| 126 | + // Check subdomain handling |
| 127 | + const subdomainLink = await page.$( |
| 128 | + 'a[href*="sub.example.com"]:not([href*="www."])', |
| 129 | + ); |
| 130 | + const otherSubdomainLink = await page.$('a[href*="other.example.com"]'); |
| 131 | + const wwwSubdomainLink = await page.$('a[href*="www.sub.example.com"]'); |
| 132 | + |
| 133 | + const subdomainHref = await subdomainLink?.getAttribute('href'); |
| 134 | + const otherSubdomainHref = await otherSubdomainLink?.getAttribute('href'); |
| 135 | + const wwwSubdomainHref = await wwwSubdomainLink?.getAttribute('href'); |
| 136 | + |
| 137 | + expect(subdomainHref).toContain('dub_id=test-click-id'); |
| 138 | + expect(otherSubdomainHref).not.toContain('dub_id=test-click-id'); |
| 139 | + expect(wwwSubdomainHref).toContain('dub_id=test-click-id'); |
| 140 | + }); |
| 141 | + |
| 142 | + test('should handle www. prefix in iframe src', async ({ page }) => { |
| 143 | + await page.goto('/outbound?dub_id=test-click-id'); |
| 144 | + await page.waitForFunction(() => window._dubAnalytics !== undefined); |
| 145 | + |
| 146 | + await page.waitForTimeout(2500); |
| 147 | + |
| 148 | + const iframe = await page.$('iframe'); |
| 149 | + const iframeSrc = await iframe?.getAttribute('src'); |
| 150 | + expect(iframeSrc).toContain('dub_id=test-click-id'); |
| 151 | + }); |
| 152 | +}); |
0 commit comments