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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
| def run(keywords: Array[String], location_id: Integer, language_id: Integer, session: AdWordsSession, adWordsServices: AdWordsServices): Map[String, Map[String, Any]] = {
val oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS)
.fromFile("./ads.properties")
.build()
.generateCredential()
val logger = LoggerFactory.getLogger(classOf[AdWordsServices])
val session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential)
.build()
val adWordsServices = new AdWordsServices()
val ideaResults = runTargetingIdeaQuery(adWordsServices, session, keywords, location_id, language_id)
val traficEstimatorResults = runTrafficEstimatorQuery(adWordsServices, session, keywords, location_id, language_id)
var resultKeywords = Map[String, Map[String, Any]]()
for (keyword <- keywords) {
resultKeywords = resultKeywords + (keyword.toLowerCase() -> Map())
for ((ideaKey, ideaValue) <- ideaResults(keyword.toLowerCase())) {
var newVal = resultKeywords(keyword.toLowerCase()) + (ideaKey -> ideaValue)
resultKeywords = resultKeywords + (keyword.toLowerCase() -> newVal)
}
for ((ideaKey, ideaValue) <- traficEstimatorResults(keyword)) {
var newVal = resultKeywords(keyword.toLowerCase()) + (ideaKey -> ideaValue)
resultKeywords = resultKeywords + (keyword.toLowerCase() -> newVal)
}
}
return resultKeywords
}
def runTargetingIdeaQuery(adWordsServices: AdWordsServices, session: AdWordsSession, keywords: Array[String], location_id: Integer, language_id: Integer): Map[String, Map[String, Any]] = {
val targetingIdeaService = adWordsServices.get(session, classOf[TargetingIdeaServiceInterface])
val selector = new TargetingIdeaSelector()
selector.setRequestType(RequestType.STATS)
selector.setIdeaType(IdeaType.KEYWORD)
selector.setRequestedAttributeTypes(Array(AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME,
AttributeType.CATEGORY_PRODUCTS_AND_SERVICES, AttributeType.AVERAGE_CPC, AttributeType.COMPETITION))
val paging = new Paging()
paging.setStartIndex(0)
paging.setNumberResults(2500)
selector.setPaging(paging)
val relatedToQuerySearchParameter = new RelatedToQuerySearchParameter()
relatedToQuerySearchParameter.setQueries(keywords)
val languageParameter = new LanguageSearchParameter()
val language = new Language()
language.setId(language_id.toLong)
languageParameter.setLanguages(Array(language))
val locationParameter = new LocationSearchParameter()
val location = new Location()
location.setId(location_id.toLong)
locationParameter.setLocations(Array(location))
selector.setSearchParameters(Array(relatedToQuerySearchParameter, languageParameter, locationParameter))
val page = targetingIdeaService.get(selector)
var resultKeywords = Map[String, Map[String, Any]]()
if (page.getEntries != null && page.getEntries.length > 0) {
for (targetingIdea <- page.getEntries) {
val data = Maps.toMap(targetingIdea.getData)
val keyword = data.get(AttributeType.KEYWORD_TEXT).asInstanceOf[StringAttribute]
val categories = data.get(AttributeType.CATEGORY_PRODUCTS_AND_SERVICES).asInstanceOf[IntegerSetAttribute]
var categoriesString = "(none)"
if (categories != null && categories.getValue != null) {
categoriesString = categories.getValue.toJson.prettyPrint
}
val averageMonthlySearches = data.get(AttributeType.SEARCH_VOLUME).asInstanceOf[LongAttribute]
.getValue
val cpc = data.get(AttributeType.AVERAGE_CPC).asInstanceOf[MoneyAttribute]
val money = cpc.getValue
var averageCpc = 0.0
if (money != null) {
averageCpc = money.getMicroAmount.toDouble / 1000000.0
}
var result: Map[String, Any] = Map("search_volume" -> averageMonthlySearches.toString(), "categories" -> categoriesString, "average_cpc" -> averageCpc.toString())
resultKeywords = resultKeywords + (keyword.getValue() -> result)
}
} else {
println("No related keywords were found.")
}
return resultKeywords
}
def runTrafficEstimatorQuery(adWordsServices: AdWordsServices, session: AdWordsSession, keywords: Array[String], location_id: Integer, language_id: Integer): Map[String, Map[String, Any]] = {
val keywordEstimateRequests = new ArrayList[KeywordEstimateRequest]()
for (keywordText <- keywords) {
val keyword = new Keyword();
keyword.setText(keywordText);
keyword.setMatchType(KeywordMatchType.BROAD);
val keywordEstimateRequest = new KeywordEstimateRequest();
keywordEstimateRequest.setKeyword(keyword);
keywordEstimateRequests.add(keywordEstimateRequest);
}
val adGroupEstimateRequests = new ArrayList[AdGroupEstimateRequest]()
val adGroupEstimateRequest = new AdGroupEstimateRequest()
adGroupEstimateRequest.setKeywordEstimateRequests(keywordEstimateRequests.toArray(Array()))
adGroupEstimateRequest.setMaxCpc(new Money(null, 600000L))
adGroupEstimateRequests.add(adGroupEstimateRequest)
var campaignEstimateRequests = Array[CampaignEstimateRequest]()
val campaignEstimateRequest = new CampaignEstimateRequest()
campaignEstimateRequest.setAdGroupEstimateRequests(adGroupEstimateRequests.toArray(Array()))
val location = new Location()
location.setId(location_id.toLong)
val language = new Language()
language.setId(language_id.toLong)
campaignEstimateRequests = campaignEstimateRequests :+ campaignEstimateRequest
val selector = new TrafficEstimatorSelector()
selector.setCampaignEstimateRequests(campaignEstimateRequests)
val trafficEstimatorService = adWordsServices.get(session, classOf[TrafficEstimatorServiceInterface])
val result = trafficEstimatorService.get(selector)
var resultKeywords = Map[String, Map[String, Any]]()
var index = 0
for (
campaignEstimate <- result.getCampaignEstimates; adGroupEstimate <- campaignEstimate.getAdGroupEstimates;
keywordWithEstimate <- adGroupEstimate.getKeywordEstimates.zip(keywords)
) {
var keyword = keywordWithEstimate._2
var keywordEstimate = keywordWithEstimate._1
val min = keywordEstimate.getMin
val max = keywordEstimate.getMax
val avg_total_cost = (max.getTotalCost.getMicroAmount + min.getTotalCost.getMicroAmount) / 2000000.0
var result: Map[String, Any] = Map("avg_ctr" -> (min.getClickThroughRate + max.getClickThroughRate) / 2,
"keyword" -> keyword, "avg_total_cost" -> avg_total_cost.toString(),
"clicks_per_day" -> Array(min.getClicksPerDay, max.getClicksPerDay),
"total_cost" -> Array(min.getTotalCost.getMicroAmount / 1000000, max.getTotalCost.getMicroAmount / 1000000),
"average_position" -> Array(min.getAveragePosition, max.getAveragePosition))
resultKeywords = resultKeywords + (keyword -> result)
}
return resultKeywords
}
|