Google¶
Google 에서 제공하는 데이터를 수집하기 위한 모듈이다. 현재는 Google Searchconsole API의 Search Analytics query와 GoogleAds의 search_stream 을 제공한다.
https://developers.google.com/webmaster-tools/search-console-api-original/v3 https://developers.google.com/google-ads/api/reference/rpc/v9/GoogleAdsService#searchstream
- class SearchConsole(key_file=None)[소스]¶
Google SearchConsol query
인스턴스의 기본설정 값(GOOGLE_APPLICATION_CREDENTIALS)이나 service account 키파일을 사용하여 search console 서비스 객체를 생성하고 사용자 사이트(site_url)의 검색 트래픽에 대한 통계 정보를 얻을 수 있다.
https://developers.google.com/webmaster-tools/search-console-api-original/v3/searchanalytics/query
사용 예시)
https://soomgo.com에 대해 2021-11-30 ~ 2021-11-30까지 image 검색에 대한 통계정보를 받아온다. 구글 API 서비스생성시 키파일을 사용하거나 인스턴스의 기본설정을 사용한다.>>> from soomgogather.google import SearchConsole >>> site_url = "https://soomgo.com" >>> service = SearchConsole('path/service_account_key.json') >>> # when using the default setting >>> # service = SearchConsole() >>> params = { ... 'start_dt': '2021-11-01', ... 'end_dt': '2021-11-30', ... 'type': 'image' ... } >>> r = service.query(site_url, params=params) >>> if r['rows']: ... print(r['rows'])
- query(site_url, params={})[소스]¶
전달한 필터와 조건에 맞는 검색 트래픽에 대한 정보를 반환한다.
- 매개변수
params (dict) – search console의 데이터를 받아오기 위한 매개변수, start_dt, end_dt는 필수
- params:
start_dt (str) : 통계를 추출할 기간의 시작일, YYYY-MM-DD PT (UTC - 7:00/8:00) (required)
end_dt (str) : 통계를 추출할 기간의 종료일, YYYY-MM-DD PT (UTC - 7:00/8:00) (required)
- dimensions (list)결과를 그룹핑할 키, 지정안하거나 여러개를 지정가능, 결과셋의 keys값
country, device, date, page, query, searchAppearance
- row_limit (int)반환되는 결과의 row수 지정
default 1000, maximum = 25000
- start_row (int)반환되는 결과의 시작 row 번호
결과셋은 0부터 시작하는 인덱스
양수만 가능
- type (str)구글의 검색타입 필터
web(default), video, image, news, discover, googleNews(구글 뉴스 앱)
- dimension_filter_groups (list)dimensions 로 그룹핑시 필터 및 조건
groupType: 현재는 〈and〉 조건만 지원
- filters: dimension의 필터 설정 리스트
형식: (dimension name) (an operator) (a value)
dimension: 필터를 지정할 dimension 이름 (country, device, date, query, searchAppearance)
operator: contains, equals, notEquals, includingRegex, excludingRegex
expression: 필터 값
data_state (str) : all(fresh data 포함) 또는 final 값으로 지정 (대소문자 구분안함)
aggregation_type (str) : 결과의 aggregation(집합)타입, auto(default), byPage(by URI), byProperty(by property)
- class GoogleAds(credentials=None, version='v8')[소스]¶
GoogleAds search_stream_request
GoogleAds를 사용한다는 것은 GoogleAds의 API를 사용한다는 의미이기 때문에 Class 생성시 GoogleAds서비스까지 생성한다.
query data 요청시 parameter는 다른 soomgo-gather와의 프로토콜을 동일하게 하기 위해 json 형식을 사용한다.
Google developer key를 인스턴스의 기본설정 값(GOOGLE_ADS_CONFIGURATION_FILE_PATH)이나 google developer 키파일(.yaml) 또는 dict를 사용하여 GoogleAds 서비스 객체를 생성한다.
생성한 GoogleAds 인스턴스를 사용하여 쿼리를 통해 원하는 데이터에 대한 통계 정보를 얻을 수 있다.
https://developers.google.com/google-ads/api/reference/rpc/v9/SearchGoogleAdsStreamRequest
사용 예시) dict를 사용하여 GoogleAds 서비스 인스턴스를 생성한다. 키워드 퍼포먼스의 어제 데이터의 클릭수를 받아온다.
>>> from soomgogather.google import GoogleAds >>> customer_id = "123445678" >>> query = "SELECT metrics.clicks FROM keyword_view WHERE segments.date DURING YESTERDAY" >>> params = { ... 'query': query, ... 'customer_id': customer_id, >>> } >>> credentials_dict = { ... 'developer_token': '<<PUT YOUR DEVELOPER TOKEN>>', ... 'refresh_token': '<<PUT YOUR REFRESH TOKEN>>', ... 'client_id': '<<PUT YOUR CLIENT ID>>', ... 'client_secret': '<<PUT YOUR CLIENT SECRET>>', ... 'use_proto_plus': True, ... 'login_customer_id': '<<PUT YOUR LOGIN CUSTOMER ID>>', >>> } >>> # If using file >>> # credentials_dict = { 'key_file':'<<PUT YOUR KEY FILE(.yaml) PATH>>', } >>> service = GoogleAds(credentials=credentials_dict) >>> stream = service.search_stream_request(params) >>> for batch in stream: ... for row in batch.results: ... matrics = row.matrics ... print(matrics.clicks)
- search_stream_request(params={})[소스]¶
전달한 필터와 조건에 맞는 GoogleAds 정보를 받아온다.
- 매개변수
params (dict) – stream request를 위한 매개변수, query, customer_id는 필수
- params:
query (str) : 데이터를 추출할 쿼리
customer_id (str) : GoogleAds customer ID
- summary_row_setting (str, optional) - summary row에 대한 설정
UNSPECIFIED : 명시되지 않음.
UNKNOWN : 반환 요약 행의 unknown 값을 표시.
NO_SUMMARY_ROW : 요약 행을 반환하지 않음.
SUMMARY_ROW_WITH_RESULTS : 결과와 함께 요약 행을 반환. 요약 행은 마지막 배치에서만 반환(마지막 배치에는 결과가 포함되지 않음).
SUMMARY_ROW_ONLY : 요약 행만 반환하고 결과는 반환하지 않음.