Skip to content

Instantly share code, notes, and snippets.

@manhtai
Created August 8, 2019 14:52
Show Gist options
  • Save manhtai/b4bd1cf1c1201a1db8cc676ccecc4c4f to your computer and use it in GitHub Desktop.
Save manhtai/b4bd1cf1c1201a1db8cc676ccecc4c4f to your computer and use it in GitHub Desktop.
googleads python ipblock
from googleads import adwords
CAMPAIGN_ID = 'YOUR_CAMPAIGN_ID'
BLOCK_IP = '1.1.1.1'
def main(client, campaign_id, block_ip):
# Initialize appropriate service.
campaign_criterion_service = client.GetService('CampaignCriterionService',
version='v201809')
# Create campaign criterion with blockd ip
campaign_criterion = {
'campaignId': campaign_id,
'criterion': {
'xsi_type': 'IpBlock',
'type': 'IP_BLOCK',
'ipAddress': block_ip,
},
'xsi_type': 'NegativeCampaignCriterion',
}
# Create operations.
operations = [
{
'operator': 'ADD',
'operand': campaign_criterion,
}
]
# Make the mutate request.
result = campaign_criterion_service.mutate(operations)
# Display the resulting campaign criteria.
for campaign_criterion in result['value']:
print('Campaign criterion with campaign id "%s" and criterion id "%s" '
'was updated with type "%s".'
% (campaign_criterion['campaignId'],
campaign_criterion['criterion']['id'],
campaign_criterion['criterion']['type']))
if __name__ == '__main__':
# Initialize client object.
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client, CAMPAIGN_ID, BLOCK_IP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment