he client had several one-off landing pages on different domains and wanted to pull traffic to their main website through them. These landing pages did not contain any form just links to the purchase process on another domain. So evaluating the benefit of each campaign was quite a challenge because the data was in several Google analytics properties (services) and the result was only seeing the traffic from that landing page, but you could no longer link the campaigns to the goal achievement on the main site.
That’s why I got the idea for such simple landing pages to pass UTM parameters to the main website.
When does it not have to work?
- When the links are not real links, but recoded other HTML elements.
- When you have a javascript framework overriding the content. This is what Next.JS likes to do.
- When you drive clicks on the site with your own script etc.
What does the script do?
The script crawls all the links on a given page and if it finds a link to the main site and it doesn’t have UTM parameters anymore, it adds the modified UTM parameters through which the visitor came to the landing page.
Example:
I arrive at a page with UTM parameters…
https://landin-page.com/?utm_source=google&utm_medium=cpc&utm_campaign=name-campaign&utm_term=keyword&utm_content=nice-banner
There is a link on the page:
https://main-web.com/
This will be modified to:
https://main-web.com/?utm_source=google&utm_medium=bypass-cpc&utm_campaign=name-campaign&utm_term=keyword&utm_content=nice-banner
Script properties:
- The script modifies the UTM source and UTM medium parameters, the rest is forwarded unchanged. It does this in order to be able to sufficiently distinguish the forwarded trafik. I modify the utm_medium so that I know that for this ad spend I won’t get in these GA or Google ads ets.
- I added a cookie that will hold the UTM for 30min even if the user loses the UTM parameters from the URL due to walking those few microsite pages. This solves the problem of 2-3 pages per microsite to promote UTM there as well. The 30min limit is the classic GA visit length.
- There is already a version without cookies.
- There is also a setting to not overwrite UTM parameters if they are already in the URL. So it can be run multiple times and nothing happens.
- In the script the target domain is set, you can put the URL there too ;). So that the UTM parameters are not forwarded to places where you don’t want them.
When will it not work?
You are using autotagging via gclid in Google ads, so you don’t have classic UTM parameters in the URL. But you can get around that via manual tagging, here you can also put variables like campaign name etc , so such manually automated.
What doesn’t this solve?
The organics, here I take it as that is correct, if the link remains without moditifications and the landing page in the main page will be trafik as a referral given.
When to definitely not use it?
If the landing page is on the same domain as the main site. In that case, the one you have should have the same Google analytics ID and do not do anything like that.
Implementation:
In Google tag manager, insert the following script in the tag and modify the value of “targetdomain” to your main site.
No cookies required version.
This version is used to ensure that if the landing page is really a one-page page or you don’t have permission to create cookies, you just need to edit the links.
<script> (function () { var targetdomain = "yourdomain.com"; //<------- Manual Change var params = new URLSearchParams(window.location.search); var utm_params = []; params.forEach(function(value, key) { if (key.startsWith('utm_')) { if (key=='utm_medium'){utm_params.push(key+'=bypass-'+value)} else if (key=='utm_source') {utm_params.push(key+'='+window.location.hostname+'-'+value)} else {utm_params.push(key+'='+value);} } }) utm_search = utm_params.join('&'); if (utm_params.length !=0) { // links update document.querySelectorAll('a[href]').forEach(function (ele, idx) { if ((ele.href.indexOf(targetdomain) !== -1) && (ele.href.indexOf("utm_") == -1)) { ele.href = ele.href + (ele.href.indexOf('?') === -1 ? '?' : '&') + utm_search; } }); } })(); </script>
Version with Cookies
Used in case you have consented to cookies and want to distribute the UTM link on other pages of the site for 30min.
<script> function getCookie(a) { var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)'); return b ? b.pop() : ''; } (function () { var cookieminutes = 30; var targetdomain = "yourdomain.com"; //<------- Manual Change var params = new URLSearchParams(window.location.search); var utm_params = []; params.forEach(function(value, key) { if (key.startsWith('utm_')) { if (key=='utm_medium'){utm_params.push(key+'=bypass-'+value)} else if (key=='utm_source') {utm_params.push(key+'='+window.location.hostname+'-'+value)} else {utm_params.push(key+'='+value);} } }) utm_search = utm_params.join('&'); if (!utm_search) // get values from backup cookie var utm_search = getCookie("utm_bypass"); if (!!utm_search) { // cookie backup for reload var d = new Date(); d.setTime(d.getTime() + (cookieminutes * 60 * 1000)); // cookie backup var expires = "expires=" + d.toUTCString(); document.cookie = "utm_bypass" + "=" + utm_search + ";" + expires + ";path=/"; // links update document.querySelectorAll('a[href]').forEach(function (ele, idx) { if ((ele.href.indexOf(targetdomain) !== -1) && (ele.href.indexOf("utm_") == -1)) { ele.href = ele.href + (ele.href.indexOf('?') === -1 ? '?' : '&') + utm_search; } }); } })(); </script>
When to run the script?
In Google tag manager on the DOM ready action. There will already be links in the page that can be modified. For a SPA application the script would run when the url/content changes.
The script does not use any GTM functions, so it can be inserted at the end of the page and manually in the code.
If you are using the script via GTM, as part of the optimization I recommend to only run it when you want to run it. Tjs if there are UTM parameters in the URL. This will save milliseconds.