--- title: 'Google Ad Api Report' disqus: hackmd --- ###### tags: `Ads API` `Google Ads` Google Ad Api Report === [TOC] ## 筆記目的 初步了解Report APi的運作方式,還有Google Ads Query Language的特性 具體請參考: * [Google Ad Api Report](https://developers.google.com/google-ads/api/docs/reporting/overview) ## 簡介 Google AdsService 提供了兩種查詢數據的方法 1. search 方法用來獲取分頁的數據 2. search stream 方法用來獲取流式響應 而這兩種方法都需要使用一種query Language叫做 Google Ads Query Language,縮寫是GAQL 為了創造GAQL,首先要選擇一種資源類型從中獲取數據,用戶還可以使用定制的視圖(Custom Views) 來獲取彙總字段,指標數據和特定細分,從而達到不同的分析目標 ![](https://i.imgur.com/dsf0ryL.png) 如同一般的SQL語言結構,GAQL語言也是由select where order所組成,以下將示範如何撰寫一個範例,將分為六個步驟,最後一個步驟當然是打印出結果在console [詳細的GAQL介紹](https://www.youtube.com/watch?v=XvAkASBnQBo) ## 程式步驟 ### write runExample function 以下這個範例可以拿到keyword的統計資料,請注意單純這個函數並沒有辦法執行,因為缺少main function,但是以下的步驟是一個得到報表的SOP,可以搭配之前其他筆記的main方法來獲得數據 1. Create the GoogleAdsServiceClient (記得要用try包起來) 2. Create the search query string (撰寫GAQL) 3. construct the request 4. Issue the request 5. Iterate through our results set 6. print the results to the console ```java= private void runExample(GoogleAdsClient googleAdsClient, long customerId) { // 1. Create the GoogleAdsServiceClient try (GoogleAdsServiceClient googleAdsServiceClient = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { //2. Create the search query string String searchQuery = "SELECT campaign.id, " + "campaign.name, " + "ad_group.id, " + "ad_group.name, " + "ad_group_criterion.criterion_id, " + "ad_group_criterion.keyword.text, " + "ad_group_criterion.keyword.match_type, " + "metrics.impressions, " + "metrics.clicks, " + "metrics.cost_micros " + "FROM keyword_view " + "WHERE segments.date DURING LAST_7_DAYS " + "AND campaign.advertising_channel_type = 'SEARCH' " + "AND ad_group.status = 'ENABLED' " + "AND ad_group_criterion.status IN ('ENABLED', 'PAUSED') " // Limits to the 50 keywords with the most impressions in the date range. + "ORDER BY metrics.impressions DESC " + "LIMIT 50"; // 3.Constructs the SearchGoogleAdsStreamRequest. SearchGoogleAdsStreamRequest request = SearchGoogleAdsStreamRequest.newBuilder() .setCustomerId(Long.toString(customerId)) .setQuery(searchQuery) .build(); // 4.Creates and issues a search Google Ads stream request that will retrieve all of the // requested field values for the keyword. ServerStream<SearchGoogleAdsStreamResponse> stream = googleAdsServiceClient.searchStreamCallable().call(request); // 5.Iterates through the results in the stream response and prints all of the requested // field values for the keyword in each row. for (SearchGoogleAdsStreamResponse response : stream) { for (GoogleAdsRow googleAdsRow : response.getResultsList()) { Campaign campaign = googleAdsRow.getCampaign(); AdGroup adGroup = googleAdsRow.getAdGroup(); AdGroupCriterion adGroupCriterion = googleAdsRow.getAdGroupCriterion(); Metrics metrics = googleAdsRow.getMetrics(); //6.print the results to the console System.out.printf( "Keyword text '%s' with " + "match type '%s' " + "and ID %d " + "in ad group '%s' " + "with ID %d " + "in campaign '%s' " + "with ID %d " + "had %d impression(s), " + "%d click(s), " + "and %d cost (in micros) " + "during the last 7 days.%n", adGroupCriterion.getKeyword().getText(), adGroupCriterion.getKeyword().getMatchType(), adGroupCriterion.getCriterionId(), adGroup.getName(), adGroup.getId(), campaign.getName(), campaign.getId(), metrics.getImpressions(), metrics.getClicks(), metrics.getCostMicros()); } } } } } ``` ## Criteria Metrics ## Segmentation 1. attribute和metric怎麼分 Ans attribute是屬性,例如 campaign.id metric 則是數據本身,例如 metrics.impressions ```sql= SELECT campaign.id, campaign.name, campaign.status, metrics.impressions, segments.date, FROM campaign WHERE campaign.status = 'PAUSED' AND metrics.impressions > 1000 AND segments.date during LAST_30_DAYS ORDER BY campaign.id ``` WHERE campaign.status 是FieldName??