Skip to content
FunnelGuard

Fix guide by FunnelsGuard

UTMs after # in a hash-router SPA: why GA4 ignores them

TRK-321Medium impactCould addBy FunnelGuardPublished Updated
On this page

What this issue is

A URL has three main parts after the domain: the path, the query string (starts with ?) and the fragment (starts with #). Campaign tags such as utm_source and click IDs such as gclid are expected in the query string, before the #.

Some single-page apps (SPAs) use hash routing, for example React Router's HashRouter, Vue Router in hash mode or older Angular setups. Their page addresses look like /#/pricing. If someone builds a campaign link by adding ?utm_source=... to the end of that address, the UTMs land inside the fragment. Browsers never send the fragment to the server, and GA4's page_location parsing does not treat it as the query string, so the campaign data is not read.

Broken vs working URLtext
Broken:  https://example.com/#/pricing?utm_source=google&utm_medium=cpc&utm_campaign=spring
Working: https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=spring#/pricing

Why it costs you leads or sales

The visitor still lands and may still convert, so nothing looks broken on the page. The damage is in reporting: paid, email and social visits are filed as direct or as a referral, and campaigns look weaker than they are. You may cut budget on channels that work, or fail to see which ads drive leads.

Ad platforms can also lose the link between click and conversion if a click ID such as gclid sits after the #. Smart bidding then learns from less data.

What happens to a hash-style campaign link
  1. Ad clickLink ends in /#/page?utm_source=google
  2. SPA loadsRouter reads the hash, page renders
  3. GA4 page_viewNo UTMs found in the query string
  4. Session sourceRecorded as direct or referral
  5. Conversion reportLead credited to the wrong channel

The campaign data is lost at the first analytics hit, so every later step is credited to the wrong source.

Where paid visits end up in reportsExample numbers
Where paid visits end up in reports (example)Reported as paid (UTMs before #): 95 % of sessions; Reported as paid (UTMs after #): 0 % of sessions; Reported as direct (UTMs after #): 90 % of sessions0367395090Reported as paid (UTM…Reported as paid (UTM…Reported as direct (U…% of sessions

Example: 100 paid visits to a hash-routed app. Illustrative numbers only.

How to check if your site has it

Check your site in about five minutes
  1. Check how your app routes

    Click through your app. If page addresses contain /#/ (for example /#/pricing), it uses hash routing.

  2. Look at your live campaign links

    Open your ad, email and social links. Any URL where ?utm_ or gclid= appears after the # is affected.

  3. Test with a tagged linkGoogle Analytics 4

    Open the link in a fresh private window, then in GA4 go to Admin > DebugView (or Reports > Realtime) and look at the session's source and medium. Direct or (not set) means the UTMs were ignored.

  4. Inspect the hit

    In browser dev tools, open the Network tab, filter by collect, and read the dl (page location) value of the GA4 request. If it holds the whole URL but no UTMs are read as campaign fields, the tags were in the fragment.

  5. Compare reports

    A high share of direct traffic landing on your SPA entry page, with little paid traffic despite ad spend, is a warning sign.

How to fix it

There are two routes. The quickest is to correct the links. The lasting one is to stop relying on the hash for routing. Do the first now and plan the second.

Fix the links and the app
  1. Rebuild campaign links with UTMs before the #

    Use the format https://example.com/?utm_source=...&utm_medium=...#/page. Update live ads, email templates and social bios.

  2. Make sure the server keeps the query string

    The root page must not redirect in a way that drops ? parameters. Test with a tagged link and watch the final URL.

  3. Rescue old links with a small script

    If you cannot change links already in market, run code early in the page that moves parameters from the hash into the real query string before analytics loads (snippet below).

  4. Plan a move to history-mode routing

    Switch to BrowserRouter (React), createWebHistory (Vue) or the Angular default, with a server rule that serves index.html for app paths. Then URLs look like /pricing?utm_source=... and work normally.

Move UTMs and click IDs from the hash to the query string (run before GA4 loads)javascript
(function () {
  var hash = window.location.hash; // e.g. #/pricing?utm_source=google
  var i = hash.indexOf('?');
  if (i === -1) return;
  var hashParams = new URLSearchParams(hash.slice(i + 1));
  var keep = ['utm_source','utm_medium','utm_campaign','utm_term','utm_content','gclid','fbclid','msclkid'];
  var search = new URLSearchParams(window.location.search);
  var moved = false;
  keep.forEach(function (k) {
    if (hashParams.has(k)) {
      if (!search.has(k)) search.set(k, hashParams.get(k));
      hashParams.delete(k);
      moved = true;
    }
  });
  if (!moved) return;
  var rest = hashParams.toString();
  var newHash = hash.slice(0, i) + (rest ? '?' + rest : '');
  var url = window.location.pathname + '?' + search.toString() + newHash;
  window.history.replaceState(null, '', url);
})();
Where to add the fix
  1. Add the script to index.html

    Put the snippet in an inline script in the head, above your analytics snippet.

  2. Update router mode

    Move to history mode when you can, and add a server fallback to index.html.

How to confirm it's fixed

Confirm with a tagged test visit
  1. Open a test link in a private window

    Use https://yoursite.com/?utm_source=test&utm_medium=cpc&utm_campaign=hashcheck#/pricing.

  2. Check GA4 DebugView or RealtimeGoogle Analytics 4

    The session should show source test and medium cpc.

  3. Test the old style link too

    Open /#/pricing?utm_source=test and confirm the script moves the tags and the source is still read.

  4. Move to another page and convert

    Navigate inside the app and submit a test lead. The conversion should stay attributed to the test campaign.

Before and after

Before

  • UTMs sit after the # in the URL
  • GA4 reads no campaign data
  • Paid and email visits show as direct

After

  • UTMs sit before the # or are moved there by script
  • GA4 sees source, medium and campaign
  • Reports credit the right channel

How to prevent it happening again

Keep campaign links clean
  • Write a UTM link rule: parameters always go before the #.
  • Use a link builder template that fixes the URL structure.
  • Test every new campaign link in a private window before launch.
  • Prefer history-mode routing for new apps.
  • Re-test tagged links after router, hosting or analytics changes.
  • Watch for sudden growth in direct traffic on your app's entry page.

How FunnelGuard handles this

FunnelGuard does not check this yet. Spotting UTMs placed after a # is a possible future check, but today it is not part of the product.

What FunnelGuard does do nearby: a robot visitor runs your real journeys, such as a lead form or checkout, on a schedule, and checks the tracking hits sent to GA4, Google Ads, Meta and other platforms, plus whether test leads reach your inbox and CRM. If a tag stops firing after a release, that can show up. It will not tell you that your campaign links are shaped wrongly.

To watch for this issue yourself, test each new campaign link in a private window and read the source in GA4 DebugView. You can also add a GA4 exploration or report that tracks the share of direct sessions on your app's entry page, and review it monthly. A free FunnelGuard plan (1 site, 2 journeys, daily checks) can still cover your main lead journey while you do this.

Questions people ask

Why is GA4 ignoring my UTM parameters?

One common cause is that the UTMs are placed after a # in the URL. GA4 reads campaign tags from the query string, which comes before the #. Move them before the # and the source and medium will be recorded.

Do UTM parameters work after a hash in the URL?

No, not for standard analytics. Everything after # is the fragment, which browsers do not send to servers and GA4 does not treat as the query string. UTMs must sit between ? and #.

How do I add UTM parameters to a React HashRouter app?

Put them in the real query string before the hash, like https://example.com/?utm_source=google&utm_medium=cpc#/pricing. The router still reads the hash route, while GA4 reads the UTMs. Moving to BrowserRouter is the cleaner long-term fix.

Why does my SPA traffic show as direct in Google Analytics?

If campaign links have their UTMs after the #, GA4 sees no campaign data and falls back to direct or referral. Other causes include redirects that strip the query string and missing UTMs on email or SMS links.

Can I fix old links that already have UTMs after the #?

Yes. Add a small script high in the head that moves utm_ and click ID parameters from the hash into the query string with history.replaceState before analytics loads. It must run before GA4 or Google Tag Manager reads the URL.

Does this also affect gclid and fbclid?

Yes. Click IDs after the # are not read as part of the query string either, so Google Ads and Meta may not link the click to a conversion. Treat them the same as UTMs.

Does FunnelGuard detect UTMs placed after the #?

Not yet. It is a possible future check. FunnelGuard currently checks journeys, tracking hits and lead delivery, so you should test campaign links yourself in GA4 DebugView.

Does FunnelGuard catch this?

Could add

Not checked yet: could be added to FunnelGuard.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.