|
| 1 | +# URL Parameter Ferrying Implementation |
| 2 | + |
| 3 | +This document describes the URL parameter ferrying functionality that has been implemented in your Sentry documentation site. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +URL parameter ferrying automatically preserves and transfers specific URL parameters from the current page to all internal links on the site. This ensures that important tracking parameters (like UTM codes, promo codes, etc.) are maintained as users navigate through the documentation. |
| 8 | + |
| 9 | +## Implemented Features |
| 10 | + |
| 11 | +### 1. Updated Parameter Patterns |
| 12 | + |
| 13 | +The parameter syncing patterns in `src/utils.ts` have been updated to focus on: |
| 14 | +- `^utm_` - UTM tracking parameters (utm_source, utm_medium, utm_campaign, etc.) |
| 15 | +- `^promo_` - Promotional parameters (promo_code, promo_id, etc.) |
| 16 | +- `code` - Generic code parameters |
| 17 | +- `ref` - Referral parameters |
| 18 | + |
| 19 | +**Previous patterns:** `[/utm_/i, /promo_/i, /gclid/i, /original_referrer/i]` |
| 20 | +**Updated patterns:** `[/^utm_/i, /^promo_/i, /code/, /ref/]` |
| 21 | + |
| 22 | +### 2. Enhanced Utility Functions |
| 23 | + |
| 24 | +**`ferryUrlParams(targetUrl, additionalParams)`** - A new utility function that: |
| 25 | +- Takes a target URL and optional additional parameters |
| 26 | +- Extracts matching parameters from the current page |
| 27 | +- Appends them to the target URL |
| 28 | +- Returns the modified URL with parameters |
| 29 | + |
| 30 | +### 3. Updated Link Components |
| 31 | + |
| 32 | +**SmartLink Component** (`src/components/smartLink.tsx`): |
| 33 | +- Now automatically ferries parameters to internal links |
| 34 | +- External links remain unchanged |
| 35 | +- Preserves existing functionality while adding parameter ferrying |
| 36 | + |
| 37 | +**NavLink Component** (`src/components/navlink.tsx`): |
| 38 | +- Navigation links now automatically include ferried parameters |
| 39 | +- Maintains the existing Button styling and behavior |
| 40 | + |
| 41 | +### 4. Automatic Parameter Ferrying Component |
| 42 | + |
| 43 | +**ParamFerry Component** (`src/components/paramFerry.tsx`): |
| 44 | +- A client-side component that automatically processes all links on the page |
| 45 | +- Uses MutationObserver to handle dynamically added links |
| 46 | +- Skips external links, anchors, mailto, and tel links |
| 47 | +- Marks processed links to avoid duplicate processing |
| 48 | + |
| 49 | +**Added to Layout** (`app/layout.tsx`): |
| 50 | +- The ParamFerry component is included in the root layout |
| 51 | +- Works automatically across all pages without additional setup |
| 52 | + |
| 53 | +## How It Works |
| 54 | + |
| 55 | +1. **Page Load**: When a page loads with URL parameters matching the patterns, the ParamFerry component identifies them |
| 56 | +2. **Link Processing**: All internal links on the page are processed to include the relevant parameters |
| 57 | +3. **Dynamic Updates**: New links added to the page (via JavaScript) are automatically processed |
| 58 | +4. **Navigation**: When users click internal links, they carry forward the tracked parameters |
| 59 | + |
| 60 | +## Examples |
| 61 | + |
| 62 | +### Before Implementation |
| 63 | +``` |
| 64 | +Current URL: /docs/platforms/javascript/?utm_source=google&utm_medium=cpc&code=SUMMER2024 |
| 65 | +Link on page: /docs/error-reporting/ |
| 66 | +User clicks: Goes to /docs/error-reporting/ (parameters lost) |
| 67 | +``` |
| 68 | + |
| 69 | +### After Implementation |
| 70 | +``` |
| 71 | +Current URL: /docs/platforms/javascript/?utm_source=google&utm_medium=cpc&code=SUMMER2024 |
| 72 | +Link on page: /docs/error-reporting/ (automatically becomes /docs/error-reporting/?utm_source=google&utm_medium=cpc&code=SUMMER2024) |
| 73 | +User clicks: Goes to /docs/error-reporting/?utm_source=google&utm_medium=cpc&code=SUMMER2024 (parameters preserved) |
| 74 | +``` |
| 75 | + |
| 76 | +## Usage in Components |
| 77 | + |
| 78 | +### Using the Enhanced SmartLink |
| 79 | +```tsx |
| 80 | +import {SmartLink} from 'sentry-docs/components/smartLink'; |
| 81 | + |
| 82 | +// Parameters are automatically ferried |
| 83 | +<SmartLink href="/docs/getting-started/">Get Started</SmartLink> |
| 84 | +``` |
| 85 | + |
| 86 | +### Using the ferryUrlParams Utility |
| 87 | +```tsx |
| 88 | +import {ferryUrlParams} from 'sentry-docs/utils'; |
| 89 | + |
| 90 | +const linkUrl = ferryUrlParams('/docs/platforms/'); |
| 91 | +// Returns URL with current page's tracked parameters appended |
| 92 | +``` |
| 93 | + |
| 94 | +### Using NavLink |
| 95 | +```tsx |
| 96 | +import {NavLink} from 'sentry-docs/components/navlink'; |
| 97 | + |
| 98 | +// Parameters are automatically ferried |
| 99 | +<NavLink href="/docs/guides/">Guides</NavLink> |
| 100 | +``` |
| 101 | + |
| 102 | +## Browser Compatibility |
| 103 | + |
| 104 | +The implementation uses: |
| 105 | +- `URLSearchParams` for parameter handling |
| 106 | +- `MutationObserver` for dynamic link detection |
| 107 | +- Modern JavaScript features supported in all current browsers |
| 108 | + |
| 109 | +## Performance Considerations |
| 110 | + |
| 111 | +- Links are processed efficiently using native DOM methods |
| 112 | +- MutationObserver is used to minimize performance impact |
| 113 | +- Processed links are marked to avoid reprocessing |
| 114 | +- Debouncing prevents excessive processing during rapid DOM changes |
| 115 | + |
| 116 | +## Customization |
| 117 | + |
| 118 | +To modify the parameter patterns, update the `paramsToSync` array in `src/utils.ts`: |
| 119 | + |
| 120 | +```typescript |
| 121 | +const paramsToSync = [/^utm_/i, /^promo_/i, /code/, /ref/]; |
| 122 | +``` |
| 123 | + |
| 124 | +Add new patterns following the same format: |
| 125 | +- Use RegExp for pattern matching (e.g., `/^custom_/i`) |
| 126 | +- Use strings for exact matches (e.g., `'tracking_id'`) |
| 127 | + |
| 128 | +## Testing |
| 129 | + |
| 130 | +Test the functionality by: |
| 131 | +1. Loading a page with tracked parameters: `http://localhost:3000/docs/?utm_source=test&code=ABC123` |
| 132 | +2. Verifying that internal links include the parameters |
| 133 | +3. Clicking links to confirm parameters are preserved |
| 134 | +4. Checking that external links remain unchanged |
| 135 | + |
| 136 | +The implementation is now active across your entire documentation site and will automatically ferry the specified URL parameters between pages. |
0 commit comments