#!/usr/bin/env python # coding: utf-8 # # Create Text Ads From Scratch # # ### Generate multiple ads by dynamically inserting words into their proper place in the text ads # # We will use Python's string formatting capabilities to dynamically generate multiple ads for large-scale campaigns. # # Online text ads usually consist of a few slots. For exmaple, the expanded text ads of Google AdWords and Bing Ads consist of the following slots: # # # # # ![](textad_diagram.png) # Here is an actual ad: # # ![](text_ad_screen_shot.png) # Lengths of the slots should be at most a certain given length. # Typically, you would want to create multiple ads, for multiple products, using the same template, but changing a word or a phrase within the ad. # # Here is an example for one ad slot: # **Template:** 'Learn { } interactively' # **Maximum length:** 35 # **Replacements:** 'graphic design', 'visual arts', 'fundamentals of user experience' # **Fallback:** 'graphic arts' (what to put in the braces if the total lengh is greater than the maximum allowed length) # # Running the function on each of the above replacements should return the following: # # Learn _graphic design_ interactively ('graphic design') # Learn _visual arts_ interactively ('visual arts') # Learn _graphic arts_ interactively ('fundamentals of user experience') # # Errors to check for: # * Make sure the length of the `template` together with the `fallback` string is at most the maximum length. # * Make sure the same is true for each of the given `replacements` strings, otherwise insert the fallback. # # Another example, with code: # In[1]: get_ipython().run_line_magic('xmode', 'plain') import pandas as pd # In[2]: template = 'Get the Latest {}' fallback = 'Mobile' replacements = ['iPhone', 'Sony XZ', 'Samsung', 'sony xperia xz premium'] max_len = 25 # In[3]: ad_list = [] if len(template.format(fallback)) > max_len: raise ValueError for r in replacements: if len(template.format(r)) <= max_len: ad_list.append(template.format(r)) else: ad_list.append(template.format(fallback)) # In[4]: ad_list # The last one was longer than `max_len` and so 'Mobile' was inserted instead of 'sony xperia xz premium'. # # Putting it in a function: # In[5]: def ad_create(template, replacements, fallback, max_len=20): if len(template.format(fallback)) > max_len: raise ValueError('template + fallback should be <= 20 chars') final_ad = [] for rep in replacements: if len(template.format(rep)) <= max_len: final_ad.append(template.format(rep)) else: final_ad.append(template.format(fallback)) return final_ad # In[6]: ad_create('My Car is a {}', ['BMW', 'Mercedes', 'Lamborghini'], 'Beauty', 20) # In[7]: # this should raise an exception ad_create('My Car is a {}', ['BMW', 'Mercedes', 'Lamborghini'], 'Beauty Beauty Beauty Beauty ', 20) # It raises a `ValueError` as it should. # Now we want to create a set of ads for a travel website. # As a reminder the default template we will be using is the expanded text ads used by Google AdWords and BingAds, which is: # # Ad Slot| Maximum Length # -------|------------------- # Headline 1| 30 # Headline 2| 30 # Description| 80 # Path1| 15 # Path2| 15 # Final URL| 1024 # # Templates we are going to use: # # Ad Slot| Maximum Length # -------|------------------- # Headline 1| Trips to {destination} # Headline 2| Prices Starting at ${price} # Description| Enjoy {destination} and Other Great Cities. Browse the Different Options Now. # Path1| {destination} # Path2| {country} # Final URL| http://www.mysite.com/trips/destinations/{destination} # # # In[8]: destinations = ['Rio de Janeiro', 'New York', 'Paris', 'Rome', 'Madrid', 'Istanbul', 'Dubai', 'Los Angeles'] countries = ['Brazil', 'USA', 'France', 'Italy', 'Spain', 'Turkey', 'UAE', 'USA'] prices = [500, 700, 600, 800, 400, 500, 800, 500] templates = { 'Headline 1': 'Trips to {}', 'Headline 2': 'Prices Starting at ${}', 'Description': 'Enjoy {} and Other Great Cities. Browse the Different Options. Start Now', 'Path1': '{}', 'Path2': '{}', 'Final URL': 'http://www.mysite.com/trips/destinations/{}', } # In[9]: h1 = ad_create(template=templates['Headline 1'], replacements=destinations, fallback='Great Cities', max_len=30) h1 # In[10]: h2 = ad_create(templates['Headline 2'], prices, '350', 30) h2 # In[11]: desc = ad_create(templates['Description'], destinations, 'This', 80) desc # In[12]: path1 = ad_create(templates['Path1'], destinations, '', 15) path1 # In[13]: path2 = ad_create(templates['Path2'], countries, '', 15) path2 # In[14]: final_url = ad_create(templates['Final URL'], destinations, '', 1024) final_url # We simply put them together in a DataFrame, together with the Campaign and Ad Group names based on the destinations (products) that we have. # In[17]: ads_df = pd.DataFrame({ 'Campaign': 'SEM_Destinations', 'Ad Group': destinations, 'Headline 1': h1, 'Headline 2': h2, 'Description': desc, 'Path 1': path1, 'Path 2': path2, 'Final URL': final_url }) ads_df = ads_df[['Campaign', 'Ad Group', 'Headline 1', 'Headline 2', 'Description', 'Path 1', 'Path 2']] ads_df # This now ready for uploading, and launching. # Of course the other big part of the account is the keywords, which also need to be generated. # # I'll cover these in another notebook. # # The function `ad_create` is now part of the [advertools](https://www.github.com/eliasdabbas/advertools) package, so you can test it!