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');


Continue Reading

Change Template Tracking URL in Google Ads To Include Campaign Name, Medium, MatchType, Keyword, Ad Group… Automatically Through Google Script

Use the following script to change all of your Google Ads Tracking Template to include all the Campaign Name, Ad Group, Medium, MatchType, Keyword Used automatically using Google Script. 

I used this script originally when I was using CallRail and wanted to automatically pull in the Keyword, Campaign Name into the CallRail Report. Instead of sitting there and changing each ad Final URL I used this script. 

function  main()
{
  //{CampaignName} ---> Campaignlevel Template
  //{AdGroupName} ----> AdgroupLevel Template
 

 
// Enter your template with at least one of {CampaignName} or {AdGroupName}
  var TrackingTemplate="{lpurl}?matchtype={matchtype}&amp;network={network}&amp;device={device}&amp;adposition={adposition}&amp;keyword={keyword}&amp;utm_source=google&amp;utm_medium=cpc&amp;utm_adgroup={AdGroupName}&amp;utm_campaign={CampaignName}";  //Example
  
var _CAMPAIGN_CONTAINS="";               //Filter by Campaign name   
var _ADGROUP_CONTAINS="";               //Filter by Adgroup name 
var STATUS="ENABLED";                    //ENABLED, PAUSED


// Hit Preview to see the changes/logs. 


//////////////////////////////////////////////  
if(TrackingTemplate.search("{CampaignName}")>0&amp;&amp;TrackingTemplate.search("{AdGroupName}")==-1)
{
var TempSplit=TrackingTemplate.split("&amp;");
for(var i in TempSplit)
{
 if(TempSplit[i].split("=").indexOf("{CampaignName}")>0)
  {var No=i;
   break;
  }
 }
var Temp=TempSplit[No].split("=");

 var campaignIterator=_CAMPAIGN_CONTAINS==""?AdWordsApp.campaigns().withCondition("Status = "+STATUS).get():AdWordsApp.campaigns().withCondition("Name contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Status = "+STATUS).get();

if(!campaignIterator.hasNext()){Logger.log("No Campaigns matched with this condition")}
while(campaignIterator.hasNext())
  {
  
  var campaign=campaignIterator.next();
  Temp[1]=campaign.getName(); 
  TempSplit.splice(No,1,Temp.join("="));
  var campaigntemplate=TempSplit.join("&amp;");;
  campaign.urls().setTrackingTemplate(campaigntemplate);
  }
  
}  

if(TrackingTemplate.search("{AdGroupName}")>0)
{
var CampaignCondition=false;  
var TempSplit=TrackingTemplate.split("&amp;");
for(var i in TempSplit)
{
 if(TempSplit[i].split("=").indexOf("{AdGroupName}")>0)
  {var No=i;}
  if(TempSplit[i].split("=").indexOf("{CampaignName}")>0)
  {var Cn=i;CampaignCondition=true;}
}
var Temp=TempSplit[No].split("=");
  
if(_ADGROUP_CONTAINS==""&amp;&amp;_CAMPAIGN_CONTAINS=="")
{var adgroupIterator=AdWordsApp.adGroups().withCondition("Status = "+STATUS).get();}
  else if(_ADGROUP_CONTAINS==""&amp;&amp;_CAMPAIGN_CONTAINS!=="")
var adgroupIterator=AdWordsApp.adGroups().withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
 else if(_ADGROUP_CONTAINS!==""&amp;&amp;_CAMPAIGN_CONTAINS!=="")
var adgroupIterator=AdWordsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();  
  var adgroupIterator=AdWordsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
  if(!adgroupIterator.hasNext()){Logger.log("No Campaigns/Adgroups matched with this condition")}
  if(CampaignCondition==false){
  while(adgroupIterator.hasNext())
  {   
  var adgroup=adgroupIterator.next();
  Temp[1]=adgroup.getName();
  TempSplit.splice(No,1,Temp.join("="));
  var adgrouptemplate=TempSplit.join("&amp;");
    adgroup.urls().setTrackingTemplate(adgrouptemplate);
  }
  } else{
    var TempCamp=TempSplit[Cn].split("=");
    while(adgroupIterator.hasNext())
  {   
  var adgroup=adgroupIterator.next();
  Temp[1]=encodeURIC(adgroup.getName());
  TempCamp[1]=encodeURIC(adgroup.getCampaign().getName()); 
  TempSplit.splice(No,1,Temp.join("="));
  TempSplit.splice(Cn,1,TempCamp.join("="));  
  var adgrouptemplate=TempSplit.join("&amp;");
    adgroup.urls().setTrackingTemplate(adgrouptemplate);
  }
  }
} else {Logger.log("Enter at least one of the {CampaignName} or {AdGroupName}")}    

}    


function encodeURIC( r ) {
return r.replace(/\W+/g, "");
}
Continue Reading