Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sample Salesforce web-to-lead form that captures Adwords GCLID in a hidden form field & Google Analytics Visitor ID in another hidden form field. Make sure to replace xxxxx, yyyyy, zzzzz with your specific Salesforce field values. Include your Google Analytics Tracking ID. Create a User Scope custom dimension in your Google Analytics property Ad…
<!DOCTYPE html>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
var clientId = tracker.get('clientId');
ga('send', 'pageview');
<form action="" method="POST">
window.onload = function getGclid() {
document.getElementById("xxxxx").value = (name = new
RegExp('(?:^|;\\s*)gclid=([^;]*)').exec(document.cookie)) ?
name.split(",")[1] : "";
document.getElementById("zzzzz").value = (name = new
RegExp('(?:^|;\\s*)_ga=([^;]*)').exec(document.cookie)) ?
name.split(",")[1].split(".").slice(2).join(".") : "";
<input type=hidden name="oid" value="yyyyy">
<input type=hidden name="retURL" value="http://">
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<input id="xxxxx" name="xxxxx" type="hidden" /><br>
<input id="zzzzz" name="zzzzz" type="hidden" /><br>
<input type="submit" name="submit">
<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(;
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
var gclid = getParam('gclid');
var gclsrc = getParam('gclsrc');
if(!gclsrc || gclsrc.indexOf('aw') !== -1){
setCookie('gclid', gclid, 90);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment