There is a site with internal and external links. How to automatically add utm tags to all links on the page? It is necessary that the data utm tags are transmitted to external sites.

  • Show the link generation code. And no one will help you. - E_p
  • I do not know the code for generating links. site on joomla A person gets to the site via the link mysite.ru/?utm_content=1 Do you need to go to the address bar when clicking on any links on the site? utm_content = 1 That is, gosite.ru?utm_content= was automatically added to the external link gosite.ru 1 - Alina

2 answers 2

UPD : Modified the translateLinkParam method so that you can specify several parameters for the transfer and scope:

 translateLinkParam('foo, bar', document.getElementById('change-link-params')); 

Also, the presence of hash links is now taken into account, and the existing parameters will not be duplicated (their value will be changed).

The decision with the click event listener turned out to be far from ideal - opening links through the context menu was not taken into account, and also if there were nested elements inside the link, it was impossible to focus on event.target.tagName .


You can modify the links after loading the page as follows:

 var translateLinkParam = (function() { var parseLink = function(link) { var linkObj = { path: link, hash: '', paramObj: {} }, paramPos = linkObj.path.indexOf('?'), hashPos = linkObj.path.indexOf('#'); if (hashPos > -1) { linkObj.hash = linkObj.path.substr(hashPos); linkObj.path = linkObj.path.substr(0, hashPos); } if (paramPos > -1) { linkObj.path .substr(paramPos + 1) .split('&') .forEach(function(param) { param = param.split('='); linkObj.paramObj[param[0]] = param[1]; }); linkObj.path = linkObj.path.substr(0, paramPos); } return linkObj; }, addLinkParam = function(link, paramObj) { var linkObj = parseLink(link), paramString = ''; Object.keys(paramObj).forEach(function(paramName) { linkObj.paramObj[paramName] = paramObj[paramName]; }); Object.keys(linkObj.paramObj).forEach(function(paramName) { if (paramString.length) { paramString += '&'; } paramString += paramName + '=' + linkObj.paramObj[paramName]; }); return linkObj.path + (paramString ? '?' + paramString : '') + linkObj.hash; }; return function(paramName, elem) { var locationParamObj = parseLink(window.location.href).paramObj, paramObj = {}, linkSet = elem ? elem.getElementsByTagName('a') : document.getElementsByTagName('a'), i; paramName.split(',').forEach(function(paramName) { paramName = paramName.trim(); if (locationParamObj[paramName]) { paramObj[paramName] = locationParamObj[paramName]; } }); if (Object.keys(paramObj).length) { for (i = 0; i < linkSet.length; i++) { linkSet[i].href = addLinkParam(linkSet[i].href, paramObj); } } } })(); document.addEventListener('DOMContentLoaded', function() { translateLinkParam('utm_content'); }); 

However, for dynamically generated content, you will have to perform this function after each change in the page content.

    in nginx you can change it

      rewrite ^(.*)?utm_content=(.*)$ $1?utm_content=$2; 

    On Apache2, you can also do this. I think the idea is clear - to do a redirect means server.