How to Track Redirects in Google Analytics 4
Ever move a popular page on your site, set up a 301 redirect, and then watch in horror as your campaign traffic from that old URL flatlines? Forgetting to properly track redirects is a common way to lose valuable attribution data in Google Analytics 4. This tutorial will walk you through how to track those redirects, so you never lose sight of which campaigns are driving real results.
Why Redirects Break Analytics Tracking
When a user clicks a link with campaign parameters - like yourwebsite.com/old-page?utm_source=newsletter - and that URL redirects them, the browser sometimes fails to pass that tracking information along to the new page. The redirect essentially "breaks" the chain of data that connects the click to the final page view.
Here’s what typically gets lost:
- UTM Parameters: The familiar
utm_source,utm_medium, andutm_campaigntags disappear. GA4 can’t see where the user actually came from, so it defaults to categorizing the session as Direct / (none). This makes your marketing efforts look less effective than they are. - Ad-Specific Click IDs: Advertising platforms like Google Ads (gclid) and Facebook Ads (fbclid) append their own IDs to URLs for conversion tracking. When these are stripped during a redirect, your ad platforms can no longer confirm which clicks led to sales or signups, skewing your performance data.
- The Original Referrer: The initial source of the traffic - be it a social media post, a partner website, or a search engine - often gets replaced by your own domain as the referrer. This hides the true origin story of your visitors.
Without this data, it's impossible to know which ads, emails, or content pieces are driving traffic, leaving you unable to make informed decisions about where to invest your marketing budget.
How to Track Redirects in GA4 with Google Tag Manager
The most reliable way to solve this is by capturing the full, original URL (with all its parameters) and sending it to Google Analytics 4 as a custom event parameter. We'll use Google Tag Manager (GTM) to set this up. It requires a few steps, but once it's done, it will work automatically for all future redirects.
The goal is to capture the full referring URL right before it gets lost and save it as a custom dimension in GA4 named redirect_source.
Step 1: Check if Your Traffic Source Is Being Lost
First, confirm you actually have a problem. Use the GA4 "Realtime" report to test your tagged links.
- Go to one of your old URLs that redirects to a new page. Use an incognito browser window and a link with test UTMs, like:
yourwebsite.com/old-product-page?utm_source=test&utm_medium=redirect_test. - Go to your GA4 account and navigate to Reports > Realtime.
- Look for your own activity on the map. Find the "Event parameter" card and look at the
page_viewandsession_startevents. See what GA4 has recorded as thesource,medium, andcampaignfor your visit.
If they show as "Direct / (none)" or if the campaign isn't listed, your redirect is stripping the parameters. Now we can fix it.
Step 2: Create a Custom JavaScript Variable in GTM
Google Tag Manager will run a small script to check the page's original referrer. If the referrer is from a different domain than your own, it means the user came from an external source and landed on a URL that redirected them. This variable will capture that original source.
- In your Google Tag Manager container, go to Variables in the left-hand navigation.
- In the "User-Defined Variables" section, click New.
- Name your variable something clear, like
cjs-redirect-source-and-params. - For the variable type, choose Custom JavaScript.
- Paste the following code into the code box:
function getRedirectSource() {
try {
var referrer = document.referrer,
a = document.createElement('a'),
a.href = referrer,
// Check if the referrer hostname is different from the current page's hostname
if (a.hostname !== window.location.hostname && a.hostname) {
if (a.search) {
// Return protocol, hostname, pathname, and search parameters if they exist
return a.protocol + '//' + a.hostname + a.pathname + a.search,
}
return a.protocol + '//' + a.hostname + a.pathname,
}
} catch (e) {
// Return undefined in case of an error to prevent issues
return undefined,
}
// Return undefined if it's not an external redirect
return undefined,
}- Click Save.
Step 3: Modify Your GA4 Configuration Tag
Next, we need to add our new variable as a parameter to your main GA4 Configuration Tag (or GA4 Event Tag if you send page_view events manually). This tells GTM to send the captured redirect source data to Google Analytics every time a page loads.
- In GTM, navigate to Tags.
- Find and click on your main GA4 Configuration Tag. This is often named something like "GA4 Config - G-XXXXXXXX".
- Click on Tag Configuration to edit it.
- Under Fields to Set, click Add Row. (If you don't use 'Fields to Set' you may need to use 'Set user properties', depending on your setup)
- In the Field Name column, enter
redirect_source. (Important: use lowercase with an underscore, as this will match the dimension name we'll create in GA4.) - In the Value column, click the Lego-block icon and select the Custom JavaScript variable you created in Step 2 (e.g.,
{{cjs-redirect-source-and-params}}).
By adding this, our magic Custom Javascript variable ({{cjs-redirect-source-and-params}}) will be checked with every analytics event call you have set up. Since our code returns 'undefined' when they are coming from somewhere on our site, we're making GTM only look at traffic from other websites or apps. This also makes the custom JS super easy to set up - there are a bunch of different ways to get this accomplished, but this gets the job done without getting too crazy inside of Tag Manager...
Step 4: Create the Custom Dimension in GA4
You've told GTM to send the redirect_source data, but GA4 doesn't know what to do with it yet. We need to register it as a custom dimension so you can see it in your reports.
- Navigate to your Google Analytics 4 property.
- In the bottom-left corner, click on Admin (the gear icon).
- In the "Data display" column, click on Custom definitions.
- Click the Create custom dimensions button.
- Configure the dimension as follows:
<em>Click "Save."</em> Custom Dimensions are a really common way to level up your Google skills. A bit of JS code and GTM to match them on page loads can unlock all kinds of real power from your site for free, using all of Google's great Tag tools without a lot of cost if you put GA4 + GA4 reporting on top... which can make just about any report you want now you know how to add your own data and events.</p>
Step 5: Test, Preview, and Publish
Finally, we need to test everything before making it live.
- In GTM, click the Preview button in the top right.
- Enter the new URL to land on that your redirects are directing traffic to and your original redirect URL from step 3. The page will open in a
DEBUGnew tab. - In the GTM Preview Mode (Tag Assistant pane), you should be able to see your
window loadedand the Google Tag inside should be lit (firing!).
After you publish, give it 24-48 hours for traffic to accumulate from your new data. I recommend using Looker Studio, which can bring in Google Analytics 4 from the data sources tab.
Final Thoughts
Tracking the messy reality of data flows from redirects used to be incredibly difficult. Fortunately, combining a tiny bit of JavaScript code with the power of Google Tag Manager allows us to capture that missing data and ensure our analytics in GA4 stay accurate and clean.
By capturing the true source of your website traffic, you can prove the value of your marketing campaigns with confidence. While tools and processes like this are powerful, they often involve manual setup and monitoring. At Graphed, we solve this reporting problem by giving your entire organization a very clear report on just how people ended up where from all our marketing materials. And once a redirect happens, their landing URL will get passed into a custom event so we can do all of our advanced reporting off of both where a user meant to go with the 'source_redirect' event firing.
With Graphed, you can turn all your team's marketing data into an automatic dashboard for whoever needs one and can handle all different data sources as long as they come to our site with clear redirect tags. We love setting it up, and if your business is looking to grow quickly and start understanding this type of data, give Graphed a try with a free trial.
Related Articles
How to Enable Data Analysis in Excel
Enable Excel's hidden data analysis tools with our step-by-step guide. Uncover trends, make forecasts, and turn raw numbers into actionable insights today!
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.