Skip to content

Instantly share code, notes, and snippets.

@smcjones
Last active October 4, 2019 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcjones/9c8c2c4b6edeaf5fa9fdaacc938a7162 to your computer and use it in GitHub Desktop.
Save smcjones/9c8c2c4b6edeaf5fa9fdaacc938a7162 to your computer and use it in GitHub Desktop.
Google Ads MCC Linker Example
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This example gets the account hierarchy under the current account.
The LoadFromStorage method is pulling credentials and properties from a
"googleads.yaml" file. By default, it looks for this file in your home
directory. For more information, see the "Caching authentication information"
section of our README.
"""
from absl import flags
from absl import app
from googleads import adwords
from googleads.adwords import AdWordsClient
FLAGS = flags.FLAGS
flags.DEFINE_list('customer-cids', None, 'A list of CIDs to approve links for.', short_name='c')
flags.DEFINE_string('manager-cid', None, 'The CID of the manager to approve.', short_name='m')
flags.mark_flag_as_required('customer-cids')
flags.mark_flag_as_required('manager-cid')
PAGE_SIZE = 500
"""Links accounts from {manager_cid} to {customer_cids}
client: An instance of an AdWordsClient
customer_cids: List of CIDs to check for links
manager_cid: A single manager CID for which to approve links from each customer_cid
return: A list of customer CIDs who have had their links approved.
"""
def link_accounts(client: AdWordsClient, manager_cid: int, customer_cids: list):
# Initialize appropriate service.
service = client.GetService(
'ManagedCustomerService', version='v201809')
invitations = service.getPendingInvitations({"clientCustomerIds":customer_cids})
approved_cids = []
linked_cids = []
unlinked_cids = []
for i in invitations:
if i['manager']['customerId'] == manager_cid:
try:
print('match made!')
approved_cids = [{
'operator': 'SET',
'operand': {
'managerCustomerId': manager_cid,
'clientCustomerId': i['client']['customerId'],
'linkStatus': 'ACTIVE',
}
}]
service.mutateLink(approved_cids)
linked_cids.append(i['client']['customerId'])
except:
unlinked_cids.append(i['client']['customerId'])
else:
print('did not find. found ', invitations[0]['manager']['customerId'])
return linked_cids,unlinked_cids
def main(argv):
if len(argv) > 1:
raise app.UsageError('Too many command-line arguments.')
manager_cid = int(FLAGS['manager-cid'].value.replace('-', ''))
customer_cids = [int(x.replace('-','')) for x in FLAGS['customer-cids'].value]
client = adwords.AdWordsClient.LoadFromStorage('google-ads.yaml')
linked_customers, unlinked_customers = link_accounts(client, manager_cid, customer_cids)
print('-----------')
if len(linked_customers) == 0 and len(unlinked_customers) == 0:
print('No CIDs need linking to', manager_cid)
else:
if len(linked_customers) > 0:
print('--- Links Complete from {0} to {1}'.format(manager_cid,
', '.join([str(s) for s in linked_customers])))
if len(unlinked_customers) > 0:
print('--- Links could not be completed for the following accounts: {0}'.format(
', '.join([str(s) for s in unlinked_customers])))
print(' This is likely due to permission errors. Please check whether your MCC has administrator access.')
print('-----------')
if __name__ == '__main__':
# Initialize client object.
adwords_client = adwords.AdWordsClient.LoadFromStorage('google-ads.yaml')
app.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment