Advertisement
prabapro

Capture Query String & send as hidden fields

Nov 28th, 2022
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     (function () {
  3.         // Uncomment below line to show the hidden fields for debugging
  4.         // document.querySelector('input[name="field[9]"').type = 'text';
  5.         // document.querySelector('input[name="field[11]"').type = 'text';
  6.  
  7.         // Below would capture the query strings from the cookie
  8.         var url = new URL({{cookie - __gtm_campaign_url}}),
  9.             params = new URLSearchParams(url.search),
  10.             utmSource = params.get('utm_source'),
  11.             utmMedium = params.get('utm_medium'),
  12.             fbclid = params.get('fbclid'),
  13.             gclid = params.get('gclid');
  14.  
  15.         // Fill the hidden fields with the required query params
  16.         // Change the selector or the variable name to send the required query params
  17.         document.querySelector('input[name="field[9]"').value = utmSource;
  18.         document.querySelector('input[name="field[11]"').value = fbclid;
  19.  
  20.         // Uncomment if you want to grab either FBCLID or GCLID & change the selector accordingly
  21.         // document.querySelector('input[name="field[11]"').value = gclid || fbclid;
  22.     })();
  23. </script>
Tags: gtm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement