naturalmohican
« Home  /  Blog

Nov 06, 2020

Building a dynamic landing page for PPC traffic

Using keyword optimisation to improve PPC quality score

581 words (Approximately a 3 minute read)

Seeing the PPC team struggle with a landing page generator, I offered to build a bespoke page that would also provide dynamic content to match the users’ search terms.

In this example, we are trying to match the users’ location from their search. The idea is to populate the location data into the page so the user feels that the page is tailored to them, and is not a generic page. In this scenario, the user might search for “Care in Birmingham”.

You can set up rules so that when a user clicks on one of your Google Ads, the search parameters are appended to the URL if they match a certain pattern. In our scenario, it might look like this:

https://www.example.com//?location={location} where {location} is equal to whatever place the user searches for.

Establish sensible default fall backs

In case anything goes wrong, first we need to ensure we fall back to a sensible default value such as “Care in your area”. Everything we want to be replaced on the page gets wrapped in a class (in this case, location).

let currentURL = window.location.href;
let defaultLocation = "your area";

const dynamicContent = {
	location: document.querySelector('.location')
};

Grab the users’ search terms from the URL

Now we need to inspect the URL, and if we find any keywords, we can save them into a variable.

const getParameterByName = function (name, url) {
	if (!url) {
		url = window.location.href;
	}
	name = name.replace(/[\[\]]/g, "\\$&");
	const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
	results = regex.exec(url);
	if (!results) {
		return null;
	}
	if (!results[2]) {
		return '';
	}
	return decodeURIComponent(results[2].replace(/\+/g, " "));
};

// Give the parameter a variable name
let dynamicLocation = getParameterByName('location');

Update the page content to match the users’ search terms

Next, we need to replace all our keyword placeholders in the page content with our new keyword variable. DOn’t forget to alter the string if you need to—here we are ensuring the first letter is a capital.

//Capitalise the first letter
function capitalizeFirstLetter(string) {
	return string.charAt(0).toUpperCase() + string.slice(1);
}

const changeContent = function (userEntry, content) {

	let genericLocation = document.getElementsByClassName("location");

	if(userEntry == dynamicLocation) {
		if (userEntry != defaultLocation) {
			for (var i = 0; i < genericLocation.length; i++) {
				genericLocation[i].innerHTML = dynamicLocation;
			}  
		}
	}
};

// Change content on a page
if (dynamicLocation == null) {
	dynamicLocation = defaultLocation;
} else {
	changeContent(dynamicLocation, dynamicContent);
}

Update the page metadata

Ensure you update the page title and keywords as well:

if (dynamicLocation != "null") {
	document.title = document.title + " " + dynamicLocation
}
document.title = capitalizeFirstLetter(document.title);

if(dynamicLocation != "your area") {
	let metaKeywords;
	let metaKeywordsContent;
	let metaKeywordsNewContent;
	if (document.querySelectorAll('meta[name="keywords"]').length > 0) {
		metaKeywords = document.querySelector('meta[name="keywords"]');
		metaKeywordsContent = metaKeywords.getAttribute('content');
		metaKeywordsNewContent = "Care in " + dynamicLocation;
		metaKeywordsNewContent = capitalizeFirstLetter(metaKeywordsNewContent);
		metaKeywords.setAttribute("content", metaKeywordsNewContent);
	}
};

let metaDescriptionDynamic = "Looking for care in " + dynamicLocation + "?..."
let metaDescription = document.querySelector('meta[name="description"]');
if(metaDescription) {
	metaDescription.setAttribute("content", metaDescriptionDynamic);
}

Maintain consistency across user journey

Finally, we may want to maintain this information if the user navigates to another PPC page in their journey through certain links. We can achieve this by adding a class—in this case, .tracked—to any links we need to send the dynamic search terms through to.

function trackQueries(){
  let newQuery = "?location=" + dynamicLocation;
  let plainHref = document.getElementsByClassName("tracked");

  let linkTracker = function(dynamicLink, dynamicURL){
    for (let i = 0; i < dynamicLink.length; i++) {
      var oldLink = dynamicLink[i].getAttribute("href");
      dynamicLink[i].setAttribute("href", oldLink + dynamicURL)
    } 
  }

  linkTracker(plainHref, newQuery);
}
trackQueries();
^ Top