from googleads import adwords
import csv
from io import StringIO
API_VERSION = 'v201809'
REPORT_TYPE_FIELDS = {
'ACCOUNT_PERFORMANCE_REPORT': [
'ExternalCustomerId',
'Impressions',
'Clicks',
'VideoViews',
'Conversions',
'AverageCpc',
'AverageCpm',
'Ctr',
# 'AverageFrequency', # isn't available
'Cost',
],
'CAMPAIGN_PERFORMANCE_REPORT': [
'CampaignId',
# data to create campaign
'ExternalCustomerId',
'CampaignName',
'CampaignGroupId',
'CampaignStatus',
'ServingStatus',
'StartDate',
'EndDate',
'AdvertisingChannelType',
'AdvertisingChannelSubType',
# statistics
'Impressions',
'Clicks',
'Engagements',
'VideoViews',
'Conversions',
'AverageCpc',
'AverageCpm',
'Ctr',
'ImpressionReach',
'AverageFrequency',
'Cost',
],
}
PAGE_SIZE = 500
class StatisticsAggregationTimeType(enum.Enum):
WEEKLY = 'weekly'
MONTHLY = 'monthly'
def get_performance_report(
customer_id,
# instance of googleads.adwords.AdWordsClient
(https://github.com/googleads/googleads-python-lib/blob/master/googleads/adwords.py#L173)
adwords_api_client,
aggregation_time_type=None,
report_type=None,
from_date=None,
to_date=None,
):
if aggregation_time_type == StatisticsAggregationTimeType.WEEKLY:
time_slice = 'Week'
if aggregation_time_type == StatisticsAggregationTimeType.MONTHLY:
time_slice = 'Month'
assert report_type in (
'ACCOUNT_PERFORMANCE_REPORT',
'CAMPAIGN_PERFORMANCE_REPORT',
)
print('getting {} for customer {}'.format(report_type, customer_id))
report_downloader = adwords_api_client.GetReportDownloader(
version=API_VERSION
)
report_fields = REPORT_TYPE_FIELDS[report_type] + [time_slice]
params = {
'reportName': 'current report',
'reportType': report_type,
'downloadFormat': 'CSV',
'selector': {
'fields': report_fields,
}
}
if from_date is None and to_date is None:
params['dateRangeType'] = 'ALL_TIME'
else:
params['dateRangeType'] = 'CUSTOM_DATE'
date_range = params['selector'].setdefault('dateRange', {})
if from_date is not None:
date_range['min'] = from_date.strftime('%Y%m%d')
if to_date is not None:
date_range['max'] = to_date.strftime('%Y%m%d')
report_stream = StringIO(report_downloader.DownloadReportAsString(
params,
client_customer_id=str(customer_id),
skip_report_header=True, # report name and dates
skip_column_header=True, # column names - will take them from
report_fields
skip_report_summary=True, # total (last line)
include_zero_impressions=True,
))
csv_reader = csv.reader(report_stream, delimiter=',')
report = [dict(zip(report_fields, report_line))
for report_line in csv_reader]
print('report is ready, len: {}'.format(len(report)))
return report
Введите код...
пятница, 1 февраля 2019 г., 14:09:25 UTC+3 пользователь
[email protected] написал:
>
> Can you send the code which fetches this data
>
> On Friday, February 1, 2019 at 3:46:01 AM UTC+5:30, [email protected]
> wrote:
>>
>> I use GoogleAds API to get CAMPAIGN_PERFORMANCE_REPORT, and the
>> information about periods in past differs from time to time. It differs
>> only in ImressionReach and AverageFrequence params: in some cases it is
>> null, in some not.
>>
>> For example: {'CampaignId': '128616739', 'ExternalCustomerId':
>> '6516505374', 'CampaignName': <NAME>, 'CampaignGroupId': '3969059',
>> 'CampaignStatus': 'paused', 'ServingStatus': 'eligible', 'StartDate':
>> '2013-04-07', 'EndDate': ' --', 'AdvertisingChannelType': 'Display',
>> 'AdvertisingChannelSubType': ' --', 'Impressions': '42643', 'Clicks': '60',
>> 'Engagements': '0', 'VideoViews': '0', 'Conversions': '0.00', 'AverageCpc':
>> '18178167', 'AverageCpm': '25577234', 'Ctr': '0.14%', 'ImpressionReach':
>> '2311', 'AverageFrequency': '18.5', 'Cost': '1090690000', 'Month':
>> '2018-01-01'}, {'CampaignId': '128616739', 'ExternalCustomerId':
>> '6516505374', 'CampaignName': <NAME>, 'CampaignGroupId': '3969059',
>> 'CampaignStatus': 'paused', 'ServingStatus': 'eligible', 'StartDate':
>> '2013-04-07', 'EndDate': ' --', 'AdvertisingChannelType': 'Display',
>> 'AdvertisingChannelSubType': ' --', 'Impressions': '42643', 'Clicks': '60',
>> 'Engagements': '0', 'VideoViews': '0', 'Conversions': '0.00', 'AverageCpc':
>> '18178167', 'AverageCpm': '25577234', 'Ctr': '0.14%', 'ImpressionReach': '
>> --', 'AverageFrequency': '0.0', 'Cost': '1090690000', 'Month': '2018-01-01'}
>>
>> Looks like a bug to me. Is it?
>>
>
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/8b2aee44-600c-4bc1-bef0-ece772985d76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.