Script to update the month of the year in many fields (ad group name, ads, kw, etc.)

11 views
Skip to first unread message

Johann Picot

unread,
Jul 10, 2019, 1:44:33 PM7/10/19
to Google Ads Scripts Forum
Hi, 
In one of our accounts we use the current month of the year (as text; e.g : "July") in ad group names, headlines, descriptions, keywords, display URL, and URL (as numbers in URL; e.g : "2019-07"). 
The tedious things is we have to manually update all of that each first day of every month. 

So what I'm looking for is a script to automate that.

*the months in text version have to be in French 
*the timezone the script will be based on to check the date must be that of Paris (UTC+2 or CEST in summer and UTC+1 or CET in winter) 

Here's the logic broken down in Python 3.7 below with some pseudo-code to replace the Adwords specific methods which I do not know :

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - -

from datetime import datetime
from dateutil.relativedelta import relativedelta
import locale

#Setting up the locale in French

locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')

#Getting the current date and month in different formats

currentDate = datetime.now().day
current_month_text = datetime.now().strftime('%B').capitalize()
current_month_url = datetime.now().strftime('%Y'"-"'%m')

#Getting the previous date and month in different formats

lastDate = datetime.now() - relativedelta(months=1)
last_month_text = format(lastDate, '%B').capitalize()
last_month_url = format(lastDate,'%Y'"-"'%m')

#Dummy campaign ID variable to use

campaign = "id123456"

#Test variables to check if it works 

essai = "les vacances de Juin"
essai_URL ="abc-2019-06"

#Conditional statement to change the old month for the current one in all desired fields when it is the first day of the month

if currentDate == 1:

#Below are two commented out tests I ran with the "essai" and "URL" variables to make sure it works as intended in Python.
#new_ads should give "les vacances de Juillet" (based on the variable "essai") while new_url should give "abc-2019-07" (based on the variable "essai_URL")

    #new_ads = essai.replace(last_month_text,current_month_text)
    #new_url = essai_URL.replace(last_month_url,current_month_url)

#Here's the pseudo-code for Adwords methods to get and update the different fields 

    new_headline2 = campaign.headline.replace(last_month_text,current_month_text)
    new_GA_name = campaign.GA.replace(last_month_text,current_month_text)
    new_KW = campaign.kw.replace(last_month_text,current_month_text)
    new_description = campaign.description.replace(last_month_text,current_month_text)
    new_display_url = campaign.display_url.replace(last_month_text,current_month_text)

#The URL is updated with the number format not text

    new_url = campaign.url.replace(last_month_url,current_month_url)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - -

Could anyone help me translate that python code / pseudo-code into something workable in javascript / Adwords script methods? 

Thank you in advance for your help 

Google Ads Scripts Forum Advisor Prod

unread,
Jul 10, 2019, 3:36:25 PM7/10/19
to adwords-scripts+apn2wqcdztzuznh8...@googlegroups.com, adwords-scripts+apn2wqcdztzuznh8...@googlegroups.co, adwords...@googlegroups.com
Hello,

Ads are not mutable in scripts with methods like 'replace'. I would recommend looking into ad customizers for a script solution for changing ads. 

The working with dates and times reference will be useful here. For example, you can get the current month with the following code:
 
  var timeZone = AdsApp.currentAccount().getTimeZone(); //Get current account's timezone
  var now = new Date(); //Date object for this moment
  var monthString = Utilities.formatDate(now, timeZone, 'MMMM'); //Date object formatted for your account's timezone

Then, you could use that month string in naming operations. For example:
  
  //Assume adGroup refers to a defined ad group (you can the ad group class reference here)
  var adGroupName = adGroup.getName(); //Current name  
  adGroup.setName(monthString + adGroupName); //Prepend current month to ad group name

Unfortunately, translating code is not within the scope of this support forum.

Regards,
Matt
Google Ads Scripts Team


ref:_00D1U1174p._5001UCc5Ro:ref
Reply all
Reply to author
Forward
0 new messages