Obtain GCLID in two GCLID Fields of different forms

0

I have two forms and in each one I have a hidden input with the same id but different name, where the adwords gclid is captured by a script, my question because when the user sends the info from the 2 form I do not capture the gclid ?.

<script type="text/javascript">
function setCookie(name, value, days){
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000)); 
var expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + value + expires;
}
function getParam(p){
var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var gclid = getParam('gclid');
if(gclid){
var gclsrc = getParam('gclsrc');
if(!gclsrc || gclsrc.indexOf('aw') !== -1){
    setCookie('gclid', gclid, 90);
}
}
</script>
<script> 
function readCookie(name) { 
  var n = name + "="; 
  var cookie = document.cookie.split(';'); 
  for(var i=0;i < cookie.length;i++) {      
  var c = cookie[i];      
  while (c.charAt(0)==' '){c = c.substring(1,c.length);}      
  if (c.indexOf(n) == 0){return c.substring(n.length,c.length);} 
  } 
  return null; 
  } 

  window.onload = function() {      
  document.getElementById('gclid_field').value = 
  readCookie('gclid'); 
  } 
  </script>
    
asked by AitorUdabe 11.10.2016 в 11:07
source

2 answers

0

What I have done has been to change the id the second input to gclid2 and in the script where we assign the gclid to the input I have added the following line.

document.getElementById('gclid_field2').value = 
readCookie('gclid'); 

Where gclid_field2 is the id of the second input.

    
answered by 14.10.2016 / 11:13
source
0

The idea of using id is that these must be unique for each page. If you have two ids with the same name, only one will be taken into account when you try to obtain it. If you want to use repeated names use a class, for example:

var element1 = document.getElementsByClassName ('myclass') [0];

    
answered by 11.10.2016 в 17:58