Reputation: 23
I want to change the phone number that is displayed on one of my website depending on if the visitor came from google adwords or through organic means.
I have got as far as finding that I should use jquery to look for the gclid url parameter that adwords uses.
For simplicity we will say that my phone number for organic and normal referrals would be 123456789 and my phone number for adwords visitors is 987654321.
This way if I get a call on 123456789 I know it is an organic hit, and 987654321 will be from adwords helping us prove our ROI.
I have a VERY BASIC understanding of code so most of the code I have found so far I get the gist of but a lot of it goes right over my head.
Would I be right in thinking that I would have to write some information to a cookie in order to show a consistent phone number on pages visited after the landing page? as the gclid will no longer be present.
The code I have found is here: How to show content based on url parameter via JavaScript? I am struggling to see how to edit this for my requirements though.
Many Thanks!
Upvotes: 2
Views: 1624
Reputation: 11968
On the first view search for the presence of the URL string, and set a cookie
if (document.location.search.match(/gclid/).length > 0) {
document.cookie = "mycookie=true; path=/";
}
Read a cookie on subsequent page views
function getCookie(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return unescape(c.substring(nameEQ.length, c.length));
}
}
return "";
}
var value = getCookie('mycookie');
if (value != '') {
// this user has the cookie, which means they came from gclid
} else {
// this user lacks the cookie, which means they came from somewhere else
}
Upvotes: 0