maximus87

GTM

Mar 13th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. // First GTM code
  2.  
  3. (function() {
  4. function setCookie(name, value, days) {
  5. var expires = "";
  6. if (days) {
  7. var date = new Date();
  8. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  9. expires = "; expires=" + date.toUTCString();
  10. }
  11. document.cookie = "g_" + name + "=" + encodeURIComponent(value) + expires + "; path=/; Secure; SameSite=Lax";
  12. }
  13.  
  14. function getQueryParam(param) {
  15. var urlParams = new RegExp('[?&]' + param + '=([^&#]*)').exec(window.location.search);
  16. return urlParams ? decodeURIComponent(urlParams[1].replace(/\+/g, ' ')) : null;
  17. }
  18.  
  19. var utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_id'];
  20. var utmIdExists = getQueryParam('utm_id') !== null;
  21. var gclidExists = getQueryParam('gclid') !== null;
  22.  
  23. if (utmIdExists || gclidExists) { // Fire only if utm_id or gclid is present
  24. utmParams.forEach(function(param) {
  25. var value = getQueryParam(param);
  26. if (value) {
  27. setCookie(param, value, 730); // 2 years expiry
  28. }
  29. });
  30. }
  31.  
  32. // Notify GTM that cookies are set
  33. window.utmCookiesSet = true;
  34. })();
  35.  
  36. // Second GTM code
  37.  
  38. (function() {
  39. function getCookie(name) {
  40. var match = document.cookie.match(new RegExp('(?:^|;\\s*)g_' + name + '=([^;]*)'));
  41. return match ? decodeURIComponent(match[1]) : "";
  42. }
  43.  
  44. function waitForCookies(callback) {
  45. if (window.utmCookiesSet) {
  46. callback();
  47. } else {
  48. setTimeout(function() { waitForCookies(callback); }, 100); // Retry every 100ms
  49. }
  50. }
  51.  
  52. waitForCookies(function() {
  53. var utm_source = getCookie("utm_source");
  54. var utm_medium = getCookie("utm_medium");
  55. var utm_campaign = getCookie("utm_campaign");
  56. var utm_term = getCookie("utm_term");
  57. var utm_content = getCookie("utm_content");
  58. var utm_id = getCookie("utm_id");
  59.  
  60. if (!utm_campaign) {
  61. console.warn("No g_utm_campaign cookie found. Skipping link updates.");
  62. return;
  63. }
  64.  
  65. var utm_query = `?utm_source=${encodeURIComponent(utm_source)}&utm_medium=${encodeURIComponent(utm_medium)}&utm_campaign=${encodeURIComponent(utm_campaign)}&utm_term=${encodeURIComponent(utm_term)}&utm_content=${encodeURIComponent(utm_content)}&utm_id=${encodeURIComponent(utm_id)}`;
  66.  
  67. var links = document.querySelectorAll("a[href]");
  68.  
  69. links.forEach(function(link) {
  70. var href = link.href;
  71.  
  72. if ((href.includes("/contact-us/") || href.includes("/products/") || href.includes("/ray-the-sheep/") || href.includes("share.hsforms.com"))
  73. && !href.includes("utm_source")) { // Avoid duplicate UTMs
  74. link.href = href.split("?")[0] + utm_query;
  75. console.log("Updated link:", link.href);
  76. }
  77. });
  78.  
  79. console.log("Selected links updated with UTM parameters.");
  80. });
  81. })();
Advertisement
Add Comment
Please, Sign In to add comment