예제 #1
0
def download_reports(credentials_path, report_type, fields, predicates):
    """
    This script demonstrates the easy interface of accessing reports per account.

    If you wish to change the accounts touched by the script you may pass an account selector to the
    adwords_service.accounts method.
    You can create an account selector with adwords_service.account_selector
    :param credentials_path: str, path to your adwords credentials file
    :param report_type: str, str, https://developers.google.com/adwords/api/docs/appendix/reports
    :param fields: list of str, columns of report
    :param predicates: list of dict, filter
    """
    # init connection to adwords API
    adwords_service = freedan.AdWordsService(credentials_path)

    report_def = adwords_service.report_definition(report_type=report_type,
                                                   fields=fields,
                                                   predicates=predicates,
                                                   last_days=7)

    # loop over accounts and download
    for account in adwords_service.accounts():
        print(account)

        report = adwords_service.download_report(report_def,
                                                 include_0_imp=True)
        print(report)  # pandas DataFrame
def add_adgroup_using_batch_job(path_credentials, is_debug):
    """
    A script that will add an adgroup with keywords and ads using batch upload.

    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        # define new adwords instances
        adgroup = AdGroup(name="test_adgroup_1")
        keyword1 = Keyword(text="asofjna",
                           match_type="EXACT",
                           bid=1.0,
                           final_url="https://aiosjda.de")
        keyword2 = Keyword(text="asofjna",
                           match_type="PHRASE",
                           bid=0.5,
                           final_url="https://aiosjda.de")
        ad1 = ExtendedTextAd(headline1="yo",
                             headline2="yoyo",
                             description="dem boys",
                             path1="Yo",
                             path2="Dude",
                             final_url="https://aiosjda.de")
        ad2 = ExtendedTextAd(headline1="yo",
                             headline2="yoyo",
                             description="dem boyoyoyoys",
                             path1="Yo",
                             path2="Dude",
                             final_url="https://aiosjda.de")

        # determine temporary id for adgroup
        temp_id_helper = freedan.TempIdHelper()
        adgroup_id = temp_id_helper.temp_id

        # operations
        adgroup_operations = [
            adgroup.add_operation(campaign_id=CAMPAIGN_ID,
                                  bid=0.01,
                                  adgroup_id=adgroup_id)
        ]
        keyword_operations = [
            keyword1.add_operation(adgroup_id=adgroup_id),
            keyword2.add_operation(adgroup_id=adgroup_id)
        ]
        ad_operations = [
            ad1.add_operation(adgroup_id=adgroup_id),
            ad2.add_operation(adgroup_id=adgroup_id)
        ]

        # upload
        operations = (adgroup_operations, keyword_operations, ad_operations)
        adwords_service.upload(operations, is_debug=is_debug, method="batch")
예제 #3
0
def add_adgroup_using_batch_job(path_credentials, is_debug):
    """
    A script that will add an adgroup with keywords and ads using batch upload.

    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        # define new adwords instances.
        # of course you can add multiple adgroups per campaign and multiple keywords/ads per adgroup.
        # check out add_adgroup_using_batch_job.py for more details
        budget = CampaignBudget(amount=10)
        campaign = Campaign(name="test_campaign_1")
        adgroup = AdGroup(name="test_adgroup_1")
        keyword = Keyword(text="asofjna",
                          match_type="EXACT",
                          bid=1.0,
                          final_url="https://aiosjda.de")
        ad = ExtendedTextAd(headline1="yo",
                            headline2="yoyo",
                            description="dem boys",
                            path1="Yo",
                            path2="Dude",
                            final_url="https://aiosjda.de")

        # determine temporary ids for batch upload
        # magic: those ids are different from another ;)
        temp_id_helper = freedan.TempIdHelper()
        budget_id = temp_id_helper.temp_id
        campaign_id = temp_id_helper.temp_id
        adgroup_id = temp_id_helper.temp_id

        # operations
        budget_operations = [budget.add_operation(temp_id=budget_id)]
        campaign_operations = [
            campaign.add_operation(budget_id=budget_id,
                                   campaign_id=campaign_id)
        ]
        # you might need additional operations for device, language or location targetings based on your business logic

        adgroup_operations = [
            adgroup.add_operation(campaign_id=campaign_id,
                                  bid=0.01,
                                  adgroup_id=adgroup_id)
        ]
        keyword_operations = [keyword.add_operation(adgroup_id=adgroup_id)]
        ad_operations = [ad.add_operation(adgroup_id=adgroup_id)]

        # upload
        operations = (budget_operations, campaign_operations,
                      adgroup_operations, keyword_operations, ad_operations)
        adwords_service.upload(operations, is_debug=is_debug, method="batch")
예제 #4
0
def account_hierarchy(credentials_path):
    """
    This script will loop over your accounts and print out the names of all accounts.
    By default MCC accounts will be skipped. You can change this by changing the 'skip_mcc' parameter
    :param credentials_path: str, path to your adwords credentials file
    """

    # init connection to adwords API
    adwords_service = freedan.AdWordsService(credentials_path)

    # access your accounts
    for account in adwords_service.accounts():
        print(account)
예제 #5
0
def keywords_without_final_url(path_credentials):
    """
    A script that finds all keywords without a final url

    Most of the time it's better to set up your final url on keyword level rather than on ad level, because keywords
    represent the user intend better.
    :param path_credentials: str, path to your adwords credentials file
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        keywords = identify_keywords_without_final_url(adwords_service)
        if keywords.empty:
            continue

        print(keywords)
예제 #6
0
def remove_empty_campaigns(path_credentials, is_debug):
    """
    A script that will delete all empty Campaigns in all accounts.
    A Campaign is considered to be empty if it doesn't contain any AdGroups.

    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        empty_campaigns = identify_empty_campaigns(adwords_service)
        # potentially save this DataFrame as a changelog

        operations = build_operations(empty_campaigns)
        adwords_service.upload(operations, is_debug=is_debug)
예제 #7
0
def update_keyword_bids(path_credentials, is_debug):
    """
    A script that will delete all empty Campaigns in all accounts.
    A Campaign is considered to be empty if it doesn't contain any AdGroups.

    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        operations = [
            Keyword.set_bid(adgroup_id=ADGROUP_ID,
                            keyword_id=KEYWORD_ID,
                            bid=NEW_BID)
        ]
        adwords_service.upload(operations, is_debug=is_debug)
예제 #8
0
def broad_to_broad_modified(path_credentials, is_debug):
    """
    A script that will look for (partially) "real" broad keywords and convert them to broad modified.

    It's good practise to avoid real broad keywords in your adwords accounts since the targeting is hard to control and
    it's very likely that your ad is showing for very unrelated searches.

    CAUTION: Since you can't change the text of a keyword in adwords, the script will delete the old keyword and
             add a new one with the fixed text. This means history for those keywords will be reset.
    CAUTION2: The new keywords will by default use https
    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        real_broads = identify_real_broads(adwords_service)

        operations = build_operations(real_broads)
        adwords_service.upload(operations, is_debug=is_debug, method="batch")
예제 #9
0
def keywords_to_lower_case(path_credentials, is_debug):
    """
    A script that will look for (partially) non-lower case keywords and convert them to lower case.

    It's good practise to keep all your keywords lower case as adwords' serving is case insensitive, but keyword ids
    aren't. Therefore you might end up with duplications or other unwanted side effects, for instance when matching
    queries and existing keywords.

    CAUTION: Since you can't change the text of a keyword in adwords, the script will delete the old keyword and
             add a new one with the fixed text. This means history for those keywords will be reset.
    CAUTION2: The new keywords will by default use https in final url
    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        non_lower_case = identify_non_lower_case(adwords_service)

        operations = build_operations(non_lower_case)
        adwords_service.upload(operations, is_debug=is_debug, method="batch")
예제 #10
0
def label_adgroups(path_credentials, is_debug):
    """
    A script that will apply a label to an AdGroup.

    To apply a label to an AdGroup you need to create the label first. You might therefore encounter one of the
    following three scenarios:
        1. label exists and you know its ID (per account)
        2. label exists, but you don't know its ID
        3. label might exist, but you're not sure.
        4. label doesn't exist yet

    freedan provides a convenient interface for any of those.

    :param path_credentials: str, path to your adwords credentials file
    :param is_debug: bool
    """
    adwords_service = freedan.AdWordsService(path_credentials)
    for account in adwords_service.accounts():
        print(account)

        ag_label = Label(LABEL_TEXT, label_id=None)

        # in case 1: provide label_id in initiation and skip Label.update_id call
        # in other cases: adapt 'action_if_not_found' parameter to your needs

        ag_label.update_id(adwords_service,
                           is_debug=is_debug,
                           action_if_not_found="create")
        operations = [
            ag_label.apply_on_adgroup_operation(adgroup_id=ADGROUP_ID)
        ]

        # upload will display an error if debug mode and label isn't existing yet
        adwords_service.upload(operations,
                               is_debug=is_debug,
                               method="standard")