/
get_account_changes.py
executable file
·99 lines (87 loc) · 3.7 KB
/
get_account_changes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
#
# 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 changes in the account during the last 24 hours.
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.
"""
import datetime
from googleads import adwords
def main(client):
# Initialize appropriate service.
customer_sync_service = client.GetService(
'CustomerSyncService', version='v201809')
campaign_service = client.GetService('CampaignService', version='v201809')
# Construct selector and get all campaigns.
selector = {
'fields': ['Id', 'Name', 'Status']
}
campaigns = campaign_service.get(selector)
campaign_ids = []
if 'totalNumEntries' in campaigns and campaigns['totalNumEntries'] > 0:
for campaign in campaigns['entries']:
campaign_ids.append(campaign['id'])
else:
print('No campaigns were found.')
return
# Construct selector and get all changes.
today = datetime.datetime.today()
yesterday = today - datetime.timedelta(1)
selector = {
'dateTimeRange': {
'min': yesterday.strftime('%Y%m%d %H%M%S'),
'max': today.strftime('%Y%m%d %H%M%S')
},
'campaignIds': campaign_ids
}
account_changes = customer_sync_service.get(selector)
# Display results.
if account_changes:
if 'lastChangeTimestamp' in account_changes:
print('Most recent changes: %s' % account_changes['lastChangeTimestamp'])
if account_changes['changedCampaigns']:
for data in account_changes['changedCampaigns']:
print('Campaign with id "%s" has change status "%s".'
% (data['campaignId'], data['campaignChangeStatus']))
if (data['campaignChangeStatus'] != 'NEW' and
data['campaignChangeStatus'] != 'FIELDS_UNCHANGED'):
if 'addedCampaignCriteria' in data:
print(' Added campaign criteria: %s' %
data['addedCampaignCriteria'])
if 'removedCampaignCriteria' in data:
print(' Removed campaign criteria: %s' %
data['removedCampaignCriteria'])
if 'changedAdGroups' in data:
for ad_group_data in data['changedAdGroups']:
print(' Ad group with id "%s" has change status "%s".'
% (ad_group_data['adGroupId'],
ad_group_data['adGroupChangeStatus']))
if ad_group_data['adGroupChangeStatus'] != 'NEW':
if 'changedAds' in ad_group_data:
print(' Changed ads: %s' % ad_group_data['changedAds'])
if 'changedCriteria' in ad_group_data:
print(' Changed criteria: %s' %
ad_group_data['changedCriteria'])
if 'removedCriteria' in ad_group_data:
print(' Removed criteria: %s' %
ad_group_data['removedCriteria'])
else:
print('No changes were found.')
if __name__ == '__main__':
# Initialize client object.
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client)