Skip to content

Instantly share code, notes, and snippets.

@hamletbatista
Created March 13, 2019 22:17
Show Gist options
  • Save hamletbatista/ab0b9ee1f28c4135c7a2a10600fb0a75 to your computer and use it in GitHub Desktop.
Save hamletbatista/ab0b9ee1f28c4135c7a2a10600fb0a75 to your computer and use it in GitHub Desktop.
from urllib.parse import unquote_plus
#standardize keyword by removing extra characters, space, url decoding and lowercase capitalization
def normalize_keywords(keyword):
bad_chars="\'\"®" #add more as needed
table = str.maketrans(dict.fromkeys(bad_chars))
#if url encoded, decode
keyword = unquote_plus(keyword)
#lower case keyword
keyword = keyword.lower()
#remove extra whitespace
import re
keyword = re.sub('[\s]+', ' ', keyword)
#remove bad chars
keyword = keyword.translate(table)
return keyword
#Here are some examples
#Check the dimensions and metrics explorer https://developers.google.com/analytics/devguides/reporting/core/dimsmets
#ga:keyword -> utm_term tracking parameter (manual campaign tracking) with quotes
ga_keyword = "'solo female traveling\""
normalize_keywords(ga_keyword)
#Returns -> 'solo female traveling'
#ga:searchKeyword -> Internal Search Term with special symbol
ga_search_keyword = "10 days in Disneyland®"
normalize_keywords(ga_search_keyword)
#Returns -> '10 days in disneyland'
#ga:searchKeywordRefinement -> Subsequent internal search term with capitalized words and extra white space
ga_search_keyword_refinement = "10 day music cities NASHVILLE to NEW ORLEANS"
normalize_keywords(ga_search_keyword_refinement)
#Returns -> '10 day music cities nashville to new orleans'
#ga:adMatchedQuery -> Search query that triggered impressions -> normalized
ga_ad_matched_query = "travel for single women"
normalize_keywords(ga_ad_matched_query)
#Returns -> 'travel for single women'
#search_url -> Google Search -> copy and pasted text, URL encoded
from_search_url = "DA+doesn’t+influence+your+Google+rankings"
normalize_keywords(from_search_url)
#Returns -> 'da doesn’t influence your google rankings'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment