WordPress – Google AdWords – Localized Information On Website

I created this plugin to be able to make a website look localized to the location defined on a Google AdWords or Facebook Ad Campaign. The plugin is able to detect the location based on the Final URL.  To Automate changing all of your Google Ads Tracking Templates take a look at this post.

To make this work you will need to define with Get Parameter the plugin will look at. For my case, it is the utm_campaign  in which it looks for the city name. An example campaign name would be Brandon School Campaign. The plugin would see the city name Brandon in the get parameter and change the information to be based on that city. This will allow you to essentially have multiple targeted/localized landing pages without actually having to create a new landing page for each location by dynamically switching the information.

<?php
/*
* Plugin Name: City Customizer ShortCode
* Description: This plugin will read get parameters and echo out the correct city information. Proper use is [grabCity output="localphone"] ouput can be any of the following, localphone, address, cityname
* Version: 1.2
* Author: Corey Jansen
* Author URI: https://coreyjansen.com
*/

//DEFINE CITY NAME AND CITY INFO
$defaultCityName = "Winnipeg";
$cityInfo = array(
	array(
		"cityname" => "Winnipeg",
		"localphone" => '<a href="tel:12045555555">(204) 555-5555</a>',
		"address" => '2080 Pembina Hwy, Winnipeg Manitoba R2E 1S5'
	) ,
	array(
		"cityname" => "Brandon",
		"localphone" => '<a href="tel:12045555555">(204) 555-5555</a>',
		"address" => '2080 Pembina Hwy, Winnipeg Manitoba R2E 1S5'
	) ,
	array(
		"cityname" => "Calgary",
		"localphone" => '<a href="tel:14035555555">(403) 555-5555</a>',
		"address" => '123 Fake Street, Calgary Alberta C2E 5S5'
	)
);



//MAIN FUNCTION THAT PULLS IN ATTRIBUTES FROM SHORTCODE AND RETURNS THE INFORMATION
function citycus_mainGetInformation($atts = [], $content = null, $tag = '')
{

	// ---------- START FUNCTION TO GET WHICH DATA WE WANT TO ECHO ----------
	// NORMALIZE ATTRIBUTE KEYS, LOWERCASE

	$atts = array_change_key_case((array)$atts, CASE_LOWER);

	// OVERRIDE DEFAULT ATTRIBUTES WITH USER ATTRIBUTES

	$wporg_atts = shortcode_atts(['output' => 'city', ], $atts, $tag);
	$output = esc_html__($wporg_atts['output']);


	$campaignName = citycus_getCityName();

	return citycus_getCityInfo($campaignName, $output);
}

//FUNCTION THAT PULLS THE CITY INFO BASED ON WHAT WAS DEFINED IN THE OUTPUT PARAMETER IN THE SHORT CODE
function citycus_getCityInfo($cityName, $returnInfo)
{
	global $cityInfo;
	foreach($cityInfo as $currentCity) {
		if ($currentCity['cityname'] == $cityName) {
			return $currentCity[$returnInfo];
		}
	}
}

// GETS CITY NAME FROM COOKIE IF SET. IF NOT GETS FROM GET PARAMETER AND
// SAVES TO COOKIE SO THAT THE PARAMETER INFORMATION IS STORED ON ALL PAGES
function citycus_getCityName()
{
	$campaignName = $_GET['utm_campaign'];
	$currentCityName = citycus_returnCityName($campaignName);
	$cookie_name = "WebsiteCampaignCity";

	// Check if cookie is set

	if (isset($_COOKIE[$cookie_name])) {
		return $_COOKIE[$cookie_name];
	}
	else {
		setcookie($cookie_name, $currentCityName, time() + (86400 * 30 * 7) , "/"); // 86400 * 7 = 7 DAYS
		return $currentCityName;
	}
}

// CHECKS GET PARAMETER AND RETURNS CITY NAME WILL RETURN DEFAULT IF NOT SET
function citycus_returnCityName($campaignName)
{
	foreach($GLOBALS['cityInfo'] as $currentCity) {
		$result = citycus_checkCityName($campaignName, $currentCity['cityname']);
		if ($result) {
			return $currentCity['cityname'];
		}
	}

	return $GLOBALS['defaultCityName'];
}
//CHECKS IF CITY NAME MATCHES
function citycus_checkCityName($campaignName, $cityName)
{
	if (strpos(strtolower($campaignName), strtolower($cityName)) !== false) {
		return true;
	}
	else {
		return false;
	}
}

//INITIATES SHORTCODE FUNCTION
function wpgrabCity_shortcodes_init()
{

	add_shortcode('grabCity', 'citycus_mainGetInformation');

}

add_action('init', 'wpgrabCity_shortcodes_init');


You may also like