How to Fix the 420-Character URL Truncation Issue in GA4

Phil Pearce
First published May 17th, 2023
Last updated July 10th, 2024
You may have noticed that URLs in Google Analytics 4 (GA4) truncate when they are over 420 characters. We've created a tool to fix that.
How to Fix the 420-Character URL Truncation Issue in GA4

The 420-character URL truncation issue

GA4 constantly introduces new features. Some of these are really handy, but not all. Recently, the GA community found a new GA4 feature that truncates all URLs longer than 420 symbols. This can cause problems.

It could affect the following dimensions:

Also, data exported in BigQuery (page_location) could be affected too. So if you query something like this:

SELECT

 max(length((SELECT ep.value.string_value from unnest(event_params) as ep where ep.key = 'page_location')))

FROM `<gcp-project-id>.analytics_<ga4-property-id>.events_<date>`

The result will be 420.

You may think these aren’t massive problems, but if your marketing team tries to optimise their campaigns and pass all utm_* params, traffic may not be attributed to the correct source/medium. 

You could easily have URLs longer than 420 characters.

 

Our solution to the 420-character URL truncation problem

We consider this a GA4 bug and hope Google will fix it soon. But they haven’t yet. So at MeasureMinds, we created a fast fix – a Google Tag Manager variable template: “URL Cleaner”.

We submitted this template in the template gallery but as a temporary solution. You can download the template from this page. In order to do this please follow these instructions:

  1. Click on the ‘download the template from this page’ link.
  2. Hit Ctrl+S to save (download) the page.
  3. Set file type to all files.
  4. Type ‘.tpl’ directly on the end of the file name.
  5. Click save.

Please see the image below to clarify things.

URL cleaner download

We have to do it this way because WordPress doesn’t allow you to upload .tpl files and GTM doesn’t allow you to import .txt files as templates.

 

How the template works

You can create a GTM variable based on the template. The variable has a Maximum URL Length property and by default, it is set to 420 characters. If your URL is longer than 420 characters, the template tries to delete query parameters until the total URL length is less than 420. 

As a bonus, the template deletes less important parameters first. Here is a list of how the query parameters are prioritised:

  1. utm_id, gclid, dclid, gbraid, wbraid.
  2. utm_source, utm_medium, utm_campaign, utm_content, utm_term.
  3. utm_marketing_tactic, utm_creative_format, utm_source_platform, utm_query, fbclid, msclkid, twclid, ttclid, li_fat_id, ScClid.

The template saves all parameters with priority 1, if the length of the URL is less than the maximum, then all parameters with priority 2 and so on.

 

How to use the template

The step-by-step plan is:

  1. Add the template to your GTM workspace.
  2. Create two GTM variables based on the template, one for page_location and the second for page_referrer.
  3. Pass these variables as page_location and page_referrer event parameters in the GA4 Configuration Tag.
  4. If your site URLs could change without the page reloading, you also need to add these parameters to GA4 page_view tags.

 

1. Add the template to your GTM workspace

Download the template from the MeasureMinds site. Select the GTM account you want to use and open the ‘Templates’ page.

Click the ‘New’ button in the Variable Templates section.

 

‘New’ button in the Variable Templates section

 

Click the 3 dots in the top-right corner and select ‘Import’ in the dropdown menu.

 

‘Import’ in the dropdown menu

 

Select ‘download template’ and after importing click the ‘Save’ button.

Now you can go to the Variables page.

 

2. Create GTM variables based on the template

First, you need to create a “Page URL for GA4 – Cleaned” variable based on the URL Cleaner template. This is used for the page_location parameter later on.

Click on ‘New’ under User-Defined Variables.


New’ under User-Defined Variables.

 

Name it “Page URL for GA4 – Cleaned”.

Click on the ‘Edit’ button and select “URL Cleaner” as variable type. This is the template you imported earlier.


Edit’ button and select “URL Cleane

 

Now it’s time to configure the variable. Follow the screenshot below.
configure the variable

 

Select {{Page URL}} under ‘Full URL’.

Select ‘Whitelist’ for ‘List Method’. This means that whatever is listed in the configuration WILL NOT be removed.

Click on ‘Add Row’ and add the following parameters and values:

 

utm_idnone
utm_sourcelower
utm_mediumlower
utm_campaignlower
utm_contentlower
utm_termlower
utm_querylower
utm_source_platformlower
utm_creative_formatlower
utm_marketing_tacticlower
gclsrcnone
gclidnone
dclidnone
gbraidsnone
wbraidsnone
_ganone
_glnone
_gacnone

 

Select ‘Clean URL’ under ‘Result Format’.

Click ‘Save’.

page_referrer

Now you need to create the next variable for page_referrer. However, we need to create other variables to achieve our end goal.

  • Referrer Hostname – www removed and lowercase

Create a new user-defined Variable

 new user-defined Variable

 

Name this Variable “Referrer Hostname – www removed and lowercase”.

Click on the ‘Edit’ button and select ‘HTTP Referrer’ as variable type.

Copy the screenshot below:

‘Edit’ button and select ‘HTTP Referrer’

 

‘Component Type’ should be ‘Host Name’.

Tick Strip ‘www’.

Select ‘Change Case to Lowercase’ under ‘Format Value’.

Click on ‘Save’.

  • Referrer Path – lowercase

Create a new user-defined Variable

Create a new user-defined Variable

 

Name this Variable “Referrer Path – lowercase”.

Click on the ‘Edit’ button and select ‘HTTP Referrer’ as the variable type.

Copy the screenshot below.

‘Edit’ button and select ‘HTTP Referrer

 

Select ‘Path’ under ‘Component Type’.

Select ‘Change Case to Lowercase’ under ‘Format Value’.

Click on ‘Save’.

  • Referrer URL – cleaned of params

Create a new user-defined Variable.

Name this Variable “Referrer URL – cleaned of params”.

Click on the ‘Edit’ button and select ‘Custom JavaScript” as the variable type.


Custom JavaScript

 

Copy the screenshot. This time you need to copy and paste a piece of JavaScript code.

 

function() {
    var gtm_referrerHostname = {{Referrer Hostname - www removed and lowercase}};
    var gtm_referrerPath = {{Referrer Path - lowercase}};
    if (gtm_referrerHostname && gtm_referrerPath) {
        return "https://" + gtm_referrerHostname + gtm_referrerPath;
    } else if (gtm_referrerHostname) {
        return "https://" + gtm_referrerHostname + "/";
    }
}

All of the variables you made earlier are now referenced to this new variable.

Click on ‘Save’.

Next phase is to add these variables in the GA4 configuration tag.

 

3. Pass these variables as a page_location and page_referrer

Go to your GA4 Configuration Tag and add these variables.

Add a row under ‘Fields to Set’. Then add ‘page_location’ under ‘Field Name’ and {{Page URL for GA4 – Cleaned}} as ‘Value’.

Add another row. This time add ‘page_referrer’ under ‘Field Name’ and {{Referrer URL – cleaned of params}} as ‘Value’.

page_referrer’ under ‘Field Name

 

Click on ‘Save’.

You’re done passing these variables!

 

4. If your site URLs could change without the page reloading

What if your website is a single-page application? The URL cleaner can still assist in cleaning the URLs. You just need to modify the configuration of the Tags and the Trigger.

Modify GA4 Configuration

Go to the GA4 Configuration Tag. Follow the screenshot below.

 

GA4 Configuration Tag

 

Make sure to untick “Send a page view event when this configuration loads”. The page_location and page_referrer are not added as parameters in this modified configuration because a separate page view tag will be created.

Click on save.

Create a New GA4 pageview tag

Create a new pageview tag. This new pageview tag contains the page_location and page_referrer.

Add a new ”Google Analytics: GA4 Event” tag and name it “GA4 page_view”

Follow the screenshot below.

GA4 Event” tag and name it “GA4 page_view

Just make sure you add your GA4 configuration tag under ‘Configuration Tag’. Add ‘page_location’ and ‘page_referrer’ the same as you did in the previous step.

Make sure that the tag only fires ‘Once per event’ under ‘Tag firing options’. This is an important option because the website in this scenario is a single-page application.

 

Once per event’ under ‘Tag firing options

 

Scroll down the tag and add a trigger. For this new GA4 pageview tag, the trigger should be ‘History Change’ with a condition that ‘History Source’ is equal to ‘popstate’.

History Source’ is equal to ‘popstat

 

Click on ‘Save’. The GA4 pageview tag now has a trigger. You may also add other triggers that are custom for your use cases.

 

GA4 pageview tag now has a trigger

 

Just remember to save any other changes that you may make.

 

What’s the impact of the URL cleaner?

If your website has very long URLs, the URL cleaner will strip the URL to an appropriate length. This will allow your URLs to be reported in GA4 without being trimmed. See the example below.

Page URL Before

 

"</p

 

Page URL After

 

"</p

 

Page Referrer Before

 

"</p

 

Page Referrer After

 

"</p

 

We tested a URL with more than 420 characters. The page_location and page_referrer parameters now have values that strip the unnecessary characters through the URL cleaner. Now the URL is usable and will appear in GA4.

In this example, the query parameters utm_source and utm_medium still appeared because they are whitelisted in the URL cleaner. The URL cleaner will remove PII in the URLs like emails. For super-long URLs, it’s possible to miss out these details.

If you find a problem in GA4 or GTM, let us know about it and we can create a tool to fix it for you.

Phil Pearce
Follow me
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Articles from our Blog
0
Would love your thoughts, please comment.x
()
x