Fix Fragment Before Query Error in 60 Seconds
Your UTMs are in the wrong order. Here's the instant fix that restores campaign tracking in Google Analytics 4.
You got an alert: "Fragment Before Query detected in your campaign URLs."
You're seeing unattributed traffic in GA4. Your campaign data is missing.
Here's the 60-second fix.
Table of contents
- The Problem (5-Second Version)
- The Fix (3 Steps - 60 Seconds Total)
- Step 1: Swap the Order (30 seconds)
- Step 2: Update Active Campaigns (20 seconds)
- Step 3: Test (10 seconds)
- Why This Happens: URL Structure Rules
- Technical Explanation
- Real Example: $5K Campaign Saved in 30 Seconds
- Common Variations of This Error
- Variation 1: Multiple Anchors
- Variation 2: SPA Hash Routing
- Variation 3: Anchor with Special Characters
- Variation 4: Encoded Fragments
- How This Mistake Happens
- Cause 1: Manual URL Construction
- Cause 2: Link Builder Tools
- Cause 3: CMS Template Errors
- Prevention: URL Construction Best Practices
- Correct URL Template
- JavaScript URL Builder
- Quick Validation Checklist
- Automation: Never Make This Mistake
- FAQ
- Will this fix my historical data?
- Do I need to restart my campaigns?
- How fast does the fix work?
- Can I skip the anchor entirely?
- What if I have multiple UTM parameters?
- Does this affect SEO?
- What about mobile deep links?
- Can fragments break other tracking?
- How do I find all broken URLs?
- What if my SPA requires hash routing?
- Related Resources
- Internal Guides
- External Resources
- Conclusion
🚨 Not sure what's breaking your tracking?
Run a free 60-second audit to check all 40+ ways UTM tracking can fail.
Scan Your Campaigns Free✓ No credit card ✓ See results instantly
The Problem (5-Second Version)
Your URL has the # symbol before the ? symbol:
❌ yoursite.com#pricing?utm_source=facebook
Everything after # stays in the browser and never reaches Google Analytics.
Result: Zero campaign tracking.
The Fix (3 Steps - 60 Seconds Total)
Step 1: Swap the Order (30 seconds)
Change this:
yoursite.com#pricing?utm_source=facebook&utm_campaign=summer
To this:
yoursite.com?utm_source=facebook&utm_campaign=summer#pricing
That's it. Question mark ? BEFORE hash #.
Step 2: Update Active Campaigns (20 seconds)
Replace the broken URL in:
- Social media posts
- Email campaigns
- Paid ads
- QR codes
No need to pause campaigns. Just update the destination URLs.
Step 3: Test (10 seconds)
- Visit your corrected URL in incognito mode
- Open GA4 → Real-Time reports
- Verify campaign appears
Done. Your tracking is fixed.
😰 Is this your only tracking issue?
This is just 1 of 40+ ways UTM tracking breaks. Most marketing teams have 8-12 critical issues they don't know about.
• 94% of sites have UTM errors
• Average: $8,400/month in wasted ad spend
• Fix time: 15 minutes with our report
✓ Connects directly to GA4 (read-only, secure)
✓ Scans 90 days of data in 2 minutes
✓ Prioritizes issues by revenue impact
✓ Shows exact sessions affected
Why This Happens: URL Structure Rules
The # symbol (fragment/anchor) is for in-page navigation. Browsers don't send anything after # to servers.
Standard URL structure:
https://domain.com/path?query_parameters#fragment
↑ ↑
Query comes first Fragment comes last
When you put UTM parameters after the hash:
domain.com#section?utm_source=facebook&utm_medium=cpc
↑ ↑
Fragment Query (WRONG ORDER!)
What the browser does:
- Sends to server:
domain.com - Keeps locally:
#section?utm_source=facebook&utm_medium=cpc
Your UTM parameters never reach Google Analytics.
Technical Explanation
How browsers parse URLs:
URL: https://shop.com/products?category=shoes&utm_source=email#reviews
Sent to server (GA4 can see):
- Domain: shop.com
- Path: /products
- Query: category=shoes&utm_source=email
Kept in browser (GA4 cannot see):
- Fragment: #reviews
With wrong order (#products?utm...):
URL: https://shop.com#products?utm_source=email
Sent to server:
- Domain: shop.com
- Path: /
- Query: (empty)
Kept in browser:
- Fragment: #products?utm_source=email
GA4 receives zero tracking data.
Real Example: $5K Campaign Saved in 30 Seconds
Campaign: LinkedIn product launch Budget: $5,000 Problem: Zero attribution after 2 days
Broken URL:
saas-app.com#features?utm_source=linkedin&utm_medium=paid-social&utm_campaign=launch
What GA4 saw:
- Source/Medium: (direct) / (none)
- Campaign: (not set)
- 2 days of spend: $714 with zero attribution
Fixed URL (30 seconds later):
saas-app.com?utm_source=linkedin&utm_medium=paid-social&utm_campaign=launch#features
Result:
- Tracking restored immediately
- Remaining 12 days tracked correctly
- Clear ROI data for future campaigns
- $4,286 in remaining spend properly attributed
Time to fix: 30 seconds Value saved: Complete attribution for $5,000 campaign
Common Variations of This Error
Variation 1: Multiple Anchors
❌ site.com#header?utm_source=email#footer
✅ site.com?utm_source=email#header
Rule: Only one anchor allowed. Place it last. Use the first anchor, discard others.
Variation 2: SPA Hash Routing
Single Page Apps often use hash routing:
❌ app.com/#/dashboard?utm_source=twitter
✅ app.com?utm_source=twitter#/dashboard
Important: Even in SPAs, query parameters must come before hash routing paths.
Variation 3: Anchor with Special Characters
❌ site.com#section-1?utm_source=facebook
✅ site.com?utm_source=facebook#section-1
Variation 4: Encoded Fragments
❌ site.com#%23pricing?utm_source=google
✅ site.com?utm_source=google#pricing
Note: Even URL-encoded fragments (%23 = #) must come after query parameters.
How This Mistake Happens
Cause 1: Manual URL Construction
Base URL: yoursite.com#pricing
Add UTMs: ?utm_source=facebook
Wrong result: yoursite.com#pricing?utm_source=facebook
Prevention: Always add query params before fragments.
Cause 2: Link Builder Tools
Some third-party URL builders don't validate fragment position:
// ❌ Wrong link builder logic
function buildUrl(base, utms, anchor) {
return base + anchor + '?' + utms;
}Cause 3: CMS Template Errors
<!-- ❌ Wrong template order -->
<a href="{{page_url}}#{{section}}?{{utm_params}}">Link</a>
<!-- ✅ Correct template order -->
<a href="{{page_url}}?{{utm_params}}#{{section}}">Link</a>Prevention: URL Construction Best Practices
Correct URL Template
https://domain.com/page?utm_source={source}&utm_medium={medium}&utm_campaign={campaign}#anchor
Order:
1. Domain and path
2. ? (question mark)
3. UTM parameters (connected with &)
4. # (hash)
5. Anchor name
JavaScript URL Builder
function buildCampaignUrl(base, utms, anchor) {
// Remove existing query/fragment
const cleanBase = base.split('?')[0].split('#')[0];
// Build correctly ordered URL
let url = cleanBase;
if (utms) {
url += '?' + utms;
}
if (anchor) {
// Remove # if already present
anchor = anchor.replace(/^#/, '');
url += '#' + anchor;
}
return url;
}
// Usage
const url = buildCampaignUrl(
'yoursite.com/page',
'utm_source=facebook&utm_medium=cpc',
'pricing'
);
// Output: yoursite.com/page?utm_source=facebook&utm_medium=cpc#pricingQuick Validation Checklist
Before launching campaigns:
✅ ? appears before # in URL
✅ All UTM parameters come after ?
✅ Anchor (if used) comes last after #
✅ Only one ? in URL
✅ Only one # in URL
✅ Test shows campaign in GA4 Real-Time
Automation: Never Make This Mistake
UTMGuard automatically:
- Scans your GA4 data for fragment-before-query errors
- Detects broken URLs before campaigns launch
- Alerts you with exact sessions affected
- Shows which campaigns have the issue
✅ Fixed this issue? Great! Now check the other 39...
You just fixed one tracking issue. But are your Google Ads doubling sessions? Is Facebook attribution broken? Are internal links overwriting campaigns?
• Connects to GA4 (read-only, OAuth secured)
• Scans 90 days of traffic in 2 minutes
• Prioritizes by revenue impact
• Free forever for monthly audits
Join 2,847 marketers fixing their tracking daily
FAQ
Will this fix my historical data?
No. Past data stays unattributed in GA4. The fix only affects future traffic from corrected URLs.
Do I need to restart my campaigns?
No. Just update the destination URLs in your ad platforms. Tracking works immediately for new clicks.
How fast does the fix work?
Instant. Test in GA4 Real-Time reports within 60 seconds of clicking the fixed URL.
Can I skip the anchor entirely?
Yes. If you don't need in-page navigation, remove the #anchor completely. UTMs will work perfectly without it.
What if I have multiple UTM parameters?
Same rule. All UTMs go after ?, connected with &, and the anchor # comes last:
domain.com?utm_source=x&utm_medium=y&utm_campaign=z&utm_content=a#anchor
Does this affect SEO?
No. Search engines understand both fragments and query parameters. The correct order doesn't impact SEO.
What about mobile deep links?
Mobile app deep links follow the same URL structure rules: app://path?params#fragment
Can fragments break other tracking?
Yes. If fragments come before query parameters, ALL query-based tracking breaks (not just UTMs):
- Affiliate tracking codes
- Referral parameters
- Custom tracking systems
How do I find all broken URLs?
Use UTMGuard to scan your GA4 data. It automatically detects fragment-before-query errors and shows affected sessions.
What if my SPA requires hash routing?
SPAs using hash routing (#/route) still follow the same rule:
✅ app.com?utm_source=email#/dashboard
❌ app.com#/dashboard?utm_source=email
Related Resources
Internal Guides
- Fix Multiple Question Marks - Double
?errors - Fix Missing Query Separator - Missing
?fix - Prevent URL Syntax Errors - Best practices
- UTM Parameters Not Working - General troubleshooting
External Resources
- RFC 3986 - URL Syntax - Official URL standard
Conclusion
Fragment Before Query error? Swap the order: ? before #.
❌ BROKEN: site.com#anchor?utm_params
✅ FIXED: site.com?utm_params#anchor
Quick fix steps:
- Find URLs with
#before? - Move
?utm_paramsto come before#anchor - Update campaign URLs
- Test in GA4 Real-Time
30 seconds. Perfect tracking restored.
Technical Reference: Fragment Before Query Validation Rule
✅ Fixed this issue? Great! Now check the other 39...
You just fixed one tracking issue. But are your Google Ads doubling sessions? Is Facebook attribution broken? Are internal links overwriting campaigns?
• Connects to GA4 (read-only, OAuth secured)
• Scans 90 days of traffic in 2 minutes
• Prioritizes by revenue impact
• Free forever for monthly audits
Join 2,847 marketers fixing their tracking daily