Javascript の初心者であるため、Google の Adwords GCLID トラッキングをサイトに実装する際に問題が発生しています。https://support.google.com/adwords/answer/2998031に示されているコード例に従っています。
保存されている Cookie を確認できますが、それを取得して非表示のフォーム フィールドにデータを入力しようとすると、結果は空白になります。他のバリエーションを使用しましたが、フィールド値として「未定義」になります。
これが私が使用しているコードです
簡易 HTML フォーム
<form class="signup-form" name="signupform" method="post" action="step2.php">
<input type="hidden" name="gclid" id="gclid" value="" />
</form>
クッキー書き込みスクリプト
<script type="text/javascript">
function setCookie(a,d,b){var c=new Date;c.setTime(c.getTime()+864E5*b);b=";
expires="+c.toGMTString();document.cookie=a+"="+d+b}function getParam(a) {return(a=RegExp("[?&]"+a+"=([^&]*)").exec(window.location.search))&&decodeURIComponent(a[1].replace(/\+/g," "))}var gclid=getParam("gclid");if(gclid){var gclsrc=getParam("gclsrc");(!gclsrc||-1!==gclsrc.indexOf("aw"))&&setCookie("gclid",gclid,90)};
</script>
Cookie 読み取りスクリプト
<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;
}
function() {
document.getElementById('gclid').value = readCookie('gclid');
}
</script>