Posts tagged with squarespace

I'm trying to pull a GCLID Parameter (EXAMPLE: https://www.example.com?gclid=CjwKCAjwiMj2BRBFEiwAYfTbCjQ-LBlWhBymAH4piPD3FalPCbl24kE7VQ4JtNJ4Jfj8XPE) from a URL and insert it into a Squarespace hidden field.

Ideally, I'd like to do this through a GTM script, but I can also add it directly into Squarespace via site-wide footer code injection. All Squarespace hidden fields start with the prefix "SQF" (in my case, the hidden field is: SQF_GCLID

I've been playing with something like this:

`<script> window.Squarespace.onInitialize(Y, function() {   /**    * Get the URL parameters    * source: https://css-tricks.com/snippets/javascript/get-url-variables/    * @param  {String} url The URL    * @return {Object}     The URL parameters    */   var getParams = function (url) {     var params = {};     var parser = document.createElement('a');     parser.href = url;     var query = parser.search.substring(1);     var vars = query.split('&');     for (var i=0; i<vars.length; i++) {       var pair = vars[i].split('=');       params[pair[0]] = decodeURIComponent(pair[1]);     }     return params;   };   /**    * Get UTM parameters from the current URL, insert them into correspondingly-named Squarespace form hidden fields.    */   var params = getParams(window.location.href);   var param;   var paramMatch;   var paramBase;   var formFields;   var i;   for (param in params) {     paramMatch = param.match(/^gclid(.*)/i);     if (!paramMatch) {         continue;     }     paramBase = paramMatch[1];     formFields = document.getElementsByName("SQF_GCLID");     i = formFields.length;     while (i--) {       if (formFields[i]) {         formFields[i].value = params[param];        }     }   } }); </script>` 

which is based on the code found here.

There are no other parameters to pass (just gclid).