| 9005 |
manish.sha |
1 |
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
package adwords.axis.v201306.reporting;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
18 |
import com.google.api.ads.adwords.lib.jaxb.v201306.DownloadFormat;
|
|
|
19 |
import com.google.api.ads.adwords.lib.jaxb.v201306.ReportDefinition;
|
|
|
20 |
import com.google.api.ads.adwords.lib.jaxb.v201306.ReportDefinitionDateRangeType;
|
|
|
21 |
import com.google.api.ads.adwords.lib.jaxb.v201306.ReportDefinitionReportType;
|
|
|
22 |
import com.google.api.ads.adwords.lib.jaxb.v201306.Selector;
|
|
|
23 |
import com.google.api.ads.adwords.lib.utils.ReportDownloadResponse;
|
|
|
24 |
import com.google.api.ads.adwords.lib.utils.ReportDownloadResponseException;
|
|
|
25 |
import com.google.api.ads.adwords.lib.utils.v201306.ReportDownloader;
|
|
|
26 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
27 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
28 |
import com.google.api.ads.common.lib.utils.Streams;
|
|
|
29 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
30 |
import com.google.common.collect.Lists;
|
|
|
31 |
|
|
|
32 |
import java.io.File;
|
|
|
33 |
import java.io.FileOutputStream;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* This example downloads a criteria performance report.
|
|
|
37 |
*
|
|
|
38 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
39 |
* "ads.properties" file. See README for more info.
|
|
|
40 |
*
|
|
|
41 |
* @author Kevin Winter
|
|
|
42 |
*/
|
|
|
43 |
public class DownloadCriteriaReport {
|
|
|
44 |
|
|
|
45 |
public static void main(String[] args) throws Exception {
|
|
|
46 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
47 |
// and can be used in place of a service account.
|
|
|
48 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
49 |
.forApi(Api.ADWORDS)
|
|
|
50 |
.fromFile()
|
|
|
51 |
.build()
|
|
|
52 |
.generateCredential();
|
|
|
53 |
|
|
|
54 |
// Construct an AdWordsSession.
|
|
|
55 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
56 |
.fromFile()
|
|
|
57 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
58 |
.build();
|
|
|
59 |
|
|
|
60 |
// Location to download report to.
|
|
|
61 |
String reportFile = System.getProperty("user.home") + File.separatorChar + "report.csv";
|
|
|
62 |
|
|
|
63 |
runExample(session, reportFile);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public static void runExample(AdWordsSession session, String reportFile) throws Exception {
|
|
|
67 |
// Create selector.
|
|
|
68 |
Selector selector = new Selector();
|
|
|
69 |
selector.getFields().addAll(Lists.newArrayList("CampaignId",
|
|
|
70 |
"AdGroupId",
|
|
|
71 |
"Id",
|
|
|
72 |
"CriteriaType",
|
|
|
73 |
"Criteria",
|
|
|
74 |
"Impressions",
|
|
|
75 |
"Clicks",
|
|
|
76 |
"Cost"));
|
|
|
77 |
|
|
|
78 |
// Create report definition.
|
|
|
79 |
ReportDefinition reportDefinition = new ReportDefinition();
|
|
|
80 |
reportDefinition.setReportName("Criteria performance report #" + System.currentTimeMillis());
|
|
|
81 |
reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.YESTERDAY);
|
|
|
82 |
reportDefinition.setReportType(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT);
|
|
|
83 |
reportDefinition.setDownloadFormat(DownloadFormat.CSV);
|
|
|
84 |
// Enable to allow rows with zero impressions to show.
|
|
|
85 |
reportDefinition.setIncludeZeroImpressions(false);
|
|
|
86 |
reportDefinition.setSelector(selector);
|
|
|
87 |
|
|
|
88 |
try {
|
|
|
89 |
// Set the property api.adwords.reportDownloadTimeout or call
|
|
|
90 |
// ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds)
|
|
|
91 |
// for CONNECT and READ in report downloads.
|
|
|
92 |
ReportDownloadResponse response =
|
|
|
93 |
new ReportDownloader(session).downloadReport(reportDefinition);
|
|
|
94 |
FileOutputStream fos = new FileOutputStream(new File(reportFile));
|
|
|
95 |
Streams.copy(response.getInputStream(), fos);
|
|
|
96 |
fos.close();
|
|
|
97 |
System.out.println("Report successfully downloaded: " + reportFile);
|
|
|
98 |
} catch (ReportDownloadResponseException e) {
|
|
|
99 |
System.out.println("Report was not downloaded. " + e.toString());
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
}
|