| 9005 |
manish.sha |
1 |
// Copyright 2013 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.v201302.adwordsforvideo;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201302.cm.DateRange;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201302.cm.Money;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201302.video.Metric;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201302.video.SegmentationDimension;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201302.video.StatsSelector;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201302.video.VideoCampaign;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201302.video.VideoCampaignPage;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201302.video.VideoCampaignSelector;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201302.video.VideoCampaignServiceInterface;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201302.video.VideoEntityStats;
|
|
|
29 |
import com.google.api.ads.adwords.axis.v201302.video.VideoEntityStatsSummaryType;
|
|
|
30 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
31 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
32 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
33 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
34 |
|
|
|
35 |
import org.joda.time.DateTime;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* This example illustrates how to retrieve stats for a video campaign.
|
|
|
39 |
*
|
|
|
40 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
41 |
* "ads.properties" file. See README for more info.
|
|
|
42 |
*
|
|
|
43 |
* Tags: VideoCampaignService.get
|
|
|
44 |
*
|
|
|
45 |
* Category: adx-exclude
|
|
|
46 |
*
|
|
|
47 |
* @author Kevin Winter
|
|
|
48 |
*/
|
|
|
49 |
public class GetVideoCampaignStats {
|
|
|
50 |
|
|
|
51 |
private static final int PAGE_SIZE = 100;
|
|
|
52 |
|
|
|
53 |
public static void main(String[] args) throws Exception {
|
|
|
54 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
55 |
// and can be used in place of a service account.
|
|
|
56 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
57 |
.forApi(Api.ADWORDS)
|
|
|
58 |
.fromFile()
|
|
|
59 |
.build()
|
|
|
60 |
.generateCredential();
|
|
|
61 |
|
|
|
62 |
// Construct an AdWordsSession.
|
|
|
63 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
64 |
.fromFile()
|
|
|
65 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
66 |
.build();
|
|
|
67 |
|
|
|
68 |
Long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
|
|
|
69 |
|
|
|
70 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
71 |
|
|
|
72 |
runExample(adWordsServices, session, campaignId);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public static void runExample(
|
|
|
76 |
AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
|
|
|
77 |
// Get the VideoCampaignService.
|
|
|
78 |
VideoCampaignServiceInterface videoCampaignService =
|
|
|
79 |
adWordsServices.get(session, VideoCampaignServiceInterface.class);
|
|
|
80 |
|
|
|
81 |
String minDateTime = new DateTime().toString("yyyy0101");
|
|
|
82 |
String maxDateTime = new DateTime().toString("yyyyMMdd");
|
|
|
83 |
|
|
|
84 |
// Create selector.
|
|
|
85 |
StatsSelector statsSelector = new StatsSelector();
|
|
|
86 |
statsSelector.setDateRange(new DateRange(minDateTime, maxDateTime));
|
|
|
87 |
statsSelector.setSegmentationDimensions(
|
|
|
88 |
new SegmentationDimension[] {SegmentationDimension.DATE_MONTH});
|
|
|
89 |
statsSelector.setMetrics(new Metric[] {Metric.VIEWS, Metric.COST, Metric.AVERAGE_CPV});
|
|
|
90 |
statsSelector.setSummaryTypes(
|
|
|
91 |
new VideoEntityStatsSummaryType[] {VideoEntityStatsSummaryType.ALL});
|
|
|
92 |
statsSelector.setSegmentedSummaryType(VideoEntityStatsSummaryType.ALL);
|
|
|
93 |
|
|
|
94 |
int offset = 0;
|
|
|
95 |
boolean morePages = true;
|
|
|
96 |
|
|
|
97 |
VideoCampaignSelector selector = new VideoCampaignSelector();
|
|
|
98 |
selector.setStatsSelector(statsSelector);
|
|
|
99 |
selector.setIds(new long[] {campaignId});
|
|
|
100 |
selector.setPaging(new Paging(offset, PAGE_SIZE));
|
|
|
101 |
|
|
|
102 |
while (morePages) {
|
|
|
103 |
// Get all campaigns.
|
|
|
104 |
VideoCampaignPage page = videoCampaignService.get(selector);
|
|
|
105 |
|
|
|
106 |
// Display campaigns.
|
|
|
107 |
if (page.getEntries() != null) {
|
|
|
108 |
for (VideoCampaign videoCampaign : page.getEntries()) {
|
|
|
109 |
System.out.printf("Campaign ID %d, name '%s' and status '%s'\n", videoCampaign.getId(),
|
|
|
110 |
videoCampaign.getName(), videoCampaign.getStatus());
|
|
|
111 |
if (videoCampaign.getStats() != null) {
|
|
|
112 |
System.out.println("\tCampaign stats:");
|
|
|
113 |
System.out.println("\t\t" + format(videoCampaign.getStats()));
|
|
|
114 |
}
|
|
|
115 |
if (videoCampaign.getSegmentedStats() != null) {
|
|
|
116 |
for (VideoEntityStats segment : videoCampaign.getSegmentedStats()) {
|
|
|
117 |
System.out.println("\tCampaign segmented stats for month of "
|
|
|
118 |
+ segment.getSegmentKey().getDateKey().getDate());
|
|
|
119 |
System.out.println("\t\t" + format(segment));
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
if (page.getSummaryStats() != null) {
|
|
|
124 |
for (VideoEntityStats summary : page.getSummaryStats()) {
|
|
|
125 |
System.out.println("\tSummary of type "
|
|
|
126 |
+ summary.getSummaryType());
|
|
|
127 |
System.out.println("\t\t" + format(summary));
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
} else {
|
|
|
131 |
System.out.println("No matching campaigns were found.");
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
offset += PAGE_SIZE;
|
|
|
135 |
selector.getPaging().setStartIndex(offset);
|
|
|
136 |
morePages = offset < page.getTotalNumEntries();
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
private static String format(VideoEntityStats stats) {
|
|
|
141 |
return String.format("Views: %s, Cost: %s, Avg. CPC: %s, Avg. CPV: %s, "
|
|
|
142 |
+ "Avg. CPM: %s, 25%%: %s, 50%%: %s, 75%%: %s, 100%%: %s",
|
|
|
143 |
dashify(stats.getViews()),
|
|
|
144 |
dashify(stats.getCost()),
|
|
|
145 |
dashify(stats.getAverageCpc()),
|
|
|
146 |
dashify(stats.getAverageCpv()),
|
|
|
147 |
dashify(stats.getAverageCpm()),
|
|
|
148 |
dashify(stats.getQuartile25Percents()),
|
|
|
149 |
dashify(stats.getQuartile50Percents()),
|
|
|
150 |
dashify(stats.getQuartile75Percents()),
|
|
|
151 |
dashify(stats.getQuartile100Percents()));
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
private static String dashify(Money m) {
|
|
|
155 |
return m == null ? "--" : String.valueOf(m.getMicroAmount());
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
private static String dashify(Number n) {
|
|
|
159 |
return n == null ? "--" : String.valueOf(n);
|
|
|
160 |
}
|
|
|
161 |
}
|