Skip to content

Commit

Permalink
modifications to kw_generate:
Browse files Browse the repository at this point in the history
- default match_types has Modified instead of Broad
- function checks for errors in match_types and max_len
- new column that adds Labels to each keyword
- new condition that creates a modified broad match keyword where applicable
  • Loading branch information
eliasdabbas committed Jan 17, 2018
1 parent d0211ff commit cf09442
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions advertools/kw_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pandas as pd

def kw_generate(products, words, max_len=3, match_types=['Exact', 'Phrase', 'Broad'],
def kw_generate(products, words, max_len=3, match_types=['Exact', 'Phrase', 'Modified'],
campaign_name='SEM_Campaign'):
"""Generate a data frame of kewywords using a list of products and relevant words.
Expand All @@ -13,27 +13,37 @@ def kw_generate(products, words, max_len=3, match_types=['Exact', 'Phrase', 'Bro
products : will be used as the names of the ad groups
words : related words that make it clear that the user is interested in `products`
max_len : the maximum number of words to include in each permutation of product keywords
match_types : can be restricted or kept as is based on preference
match_types : can be restricted or kept as is based on preference, possible values:
'Exact', 'Phrase', 'Modified', 'Broad'
campaign_name : name of campaign
>>> import advertools as adv
>>> products = ['bmw', 'toyota']
>>> words = ['buy', 'second hand']
>>> kw_df = kw_generate(products, words)
>>> kw_df = adv.kw_generate(products, words)
>>> kw_df.head()
Campaign Ad Group Keyword Criterion Type
0 SEM_Campaign bmw bmw buy Exact
1 SEM_Campaign bmw bmw buy Phrase
2 SEM_Campaign bmw bmw buy Broad
3 SEM_Campaign bmw bmw second hand Exact
4 SEM_Campaign bmw bmw second hand Phrase
Campaign Ad Group Keyword Criterion Type Labels
0 SEM_Campaign Bmw bmw buy Exact Buy
1 SEM_Campaign Bmw bmw buy Phrase Buy
2 SEM_Campaign Bmw +bmw +buy Modified Buy
3 SEM_Campaign Bmw bmw second hand Exact Second Hand
4 SEM_Campaign Bmw bmw second hand Phrase Second Hand
Returns
-------
keywords_df : a pandas.DataFrame ready to upload
"""
headers = ['Campaign', 'Ad Group', 'Keyword', 'Criterion Type']
POSSIBLE_MATCH_TYPES = ['Exact', 'Phrase', 'Broad', 'Modified']
if not all([m in POSSIBLE_MATCH_TYPES for m in match_types]):
raise ValueError('please make sure match types are any of ' + str(POSSIBLE_MATCH_TYPES))

if max_len < 2:
raise ValueError('please make sure max_len is >= 2')


headers = ['Campaign', 'Ad Group', 'Keyword', 'Criterion Type', 'Labels']
keywords_list = []
for prod in products:
for i in range(2, max_len+1):
Expand All @@ -43,9 +53,10 @@ def kw_generate(products, words, max_len=3, match_types=['Exact', 'Phrase', 'Bro
for match in match_types:
row = [
campaign_name,
prod,
' '.join(perm),
match
prod.title(),
' '.join(perm) if match != 'Modified' else '+' + ' +'.join(perm),
match,
';'.join([x.title() for x in perm if x != prod])
]
keywords_list.append(row)
return pd.DataFrame.from_records(keywords_list, columns=headers)

0 comments on commit cf09442

Please sign in to comment.