### Installing tapi-yandex-metrika Specific Version using pip Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/README.md This command installs or upgrades the 'tapi-yandex-metrika' library to a specific previous version (2020.10.20) using the pip package manager. This might be necessary for compatibility with older codebases. ```bash pip install --upgrade tapi-yandex-metrika==2020.10.20 ``` -------------------------------- ### Installing tapi-yandex-metrika Latest Version using pip Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/README.md This command installs or upgrades the 'tapi-yandex-metrika' library to the latest specified version (2022.4.8) using the pip package manager. Be aware that this version is noted as potentially having backward-incompatible changes. ```bash pip install --upgrade tapi-yandex-metrika==2022.4.8 ``` -------------------------------- ### Exploring Resources and Documentation - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/management.md Demonstrates how to discover available resources on the client object using `dir()`, get basic help information about a specific resource method using `.help()`, and open the official documentation for a resource in a browser using `.open_docs()`. These methods help understand the available API endpoints and their parameters. ```python print(dir(client)) # Help information about the method client.counters().help() # Documentation: https://yandex.ru/dev/metrika/doc/api2/management/direct_clients/getclients-docpage/ # Resource path: management/v1/clients # Available HTTP methods: # ['GET'] # Available query parameters: # 'counters=' # Open resource documentation in a browser client.counters().open_docs() ``` -------------------------------- ### Performing Basic Yandex Metrika Logs API Operations in Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/logsapi.md Initializes the Yandex Metrika Logs API client using `tapi-yandex-metrika` with an access token and counter ID. Demonstrates core API calls for checking report feasibility (`evaluate`), ordering a new report (`create`), cancelling a report, deleting a report, getting information about all reports (`allinfo`), getting details for a specific report (`info`), and conditionally downloading a specific part (`download`) once the report status is 'processed'. Requires `ACCESS_TOKEN` and `COUNTER_ID` to be set. ```python from tapi_yandex_metrika import YandexMetrikaLogsapi ACCESS_TOKEN = "" COUNTER_ID = "" client = YandexMetrikaLogsapi( access_token=ACCESS_TOKEN, default_url_params={'counterId': COUNTER_ID} ) params = { "fields": "ym:s:date,ym:s:clientID", "source": "visits", "date1": "2021-01-01", "date2": "2021-01-01" } # Check the possibility of creating a report. Via HTTP GET method. result = client.evaluate().get(params=params) print(result) # Order a report. Via HTTP POST method. result = client.create().post(params=params) request_id = result["log_request"]["request_id"] print(result) # Cancel report creation. Via HTTP POST method. result = client.cancel(requestId=request_id).post() print(result) # Delete report. Via HTTP POST method. result = client.clean(requestId=request_id).post() print(result) # Get information about all reports stored on the server. Via HTTP GET method. result = client.allinfo().get() print(result) # Get information about a specific report. Via HTTP GET method. result = client.info(requestId=request_id).get() print(result) # Download the report. Via HTTP POST method. result = client.create().post(params=params) request_id = result["log_request"]["request_id"] # The report can be downloaded when it is generated on the server. Via HTTP GET method. info = client.info(requestId=request_id).get() if info["log_request"]["status"] == "processed": # The report can consist of several parts. parts = info["log_request"]["parts"] print("Number of parts in the report", parts) # The partNumber parameter specifies the number of the part of the report that you want to download. # Default partNumber=0 part = client.download(requestId=request_id, partNumber=0).get() print("Raw data") data = part.data[:1000] print("Column names") print(part.columns) # Transform to values print(part().to_values()[:3]) # Transform to lines print(part().to_lines()[:3]) # Transform to dicts print(part().to_dicts()[:3]) else: print("Report not ready yet") ``` -------------------------------- ### Sending Various HTTP Requests - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/management.md Shows the general method calls available on a resource object (`client.counters()` in this example) for sending different types of HTTP requests (GET, POST, DELETE, PUT, PATCH, OPTIONS). The `data` parameter is used for sending request body data (e.g., for POST/PUT), and the `params` parameter is for URL querystring arguments. ```python # Send HTTP 'GET' request client.counters().get(data: dict = None, params: dict = None) # Send HTTP 'POST' request client.counters().post(data: dict = None, params: dict = None) # Send HTTP 'DELETE' request client.counters().delete(data: dict = None, params: dict = None) # Send HTTP 'PUT' request client.counters().put(data: dict = None, params: dict = None) # Send HTTP 'PATCH' request client.counters().patch(data: dict = None, params: dict = None) # Send HTTP 'OPTIONS' request client.counters().options(data: dict = None, params: dict = None) ``` -------------------------------- ### Performing API Operations (Counters, Goals) - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/management.md Provides examples of common API operations using the client, including retrieving counters (with and without parameters), creating goals using POST requests with different body structures, accessing a specific goal by ID, and performing PUT and DELETE operations on a specific goal. Demonstrates how to use the `.get()`, `.post()`, `.put()`, and `.delete()` methods with appropriate data and parameters. ```python from tapi_yandex_metrika import YandexMetrikaManagement client = YandexMetrikaManagement(...) # Get counters. Via HTTP GET method. counters = client.counters().get() print(counters.data) # Get counters sorted by visit. Via HTTP GET method. counters = client.counters().get(params={"sort": "Visits"}) print(counters.data) # Create a goal. Via HTTP POST method. body = { "goal": { "name": "2 страницы", "type": "number", "is_retargeting": 0, "depth": 2 } } client.goals().post(data=body) # Create target on JavaScript event. Via HTTP POST method. body2 = { "goal": { "name": "Название вашей цели в метрике", "type": "action", "is_retargeting": 0, "conditions": [ { "type": "exact", "url": } ] } } client.goals().post(data=body2) # For some resources, you need to substitute the object identifier in the url. # This is done by adding an identifier to the method itself. # Get information about the target. Via HTTP GET method. client.goal(goalId=10000).get() # Change target. Via HTTP PUT method. body = { "goal" : { "id" : , "name" : , "type" : , "is_retargeting" : , ... } } client.goal(goalId=10000).put(data=body) # Delete target. Via HTTP DELETE method. client.goal(goalId=10000).delete() ``` -------------------------------- ### Accessing Yandex Metrika API Response Details in Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/logsapi.md Illustrates how to access low-level response details from API calls made using `tapi-yandex-metrika`. Shows how to get the raw response data (`data`), the full underlying response object (`response`), response headers (`response.headers`), and the HTTP status code (`status_code`) for both the initial report creation response and for each individual report part downloaded. ```python from tapi_yandex_metrika import YandexMetrikaLogsapi client = YandexMetrikaLogsapi(...) info = client.create().post(params=...) print(info.data) print(info.response) print(info.response.headers) print(info.status_code) report = client.download(requestId=info["log_request"]["request_id"]).get() for part in report().parts(): print(part.data) print(part.response) print(part.response.headers) print(part.response.status_code) ``` -------------------------------- ### Directly Iterating Through All Yandex Metrika Report Rows in Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/logsapi.md Provides examples for iterating directly over all rows across all parts of a downloaded Yandex Metrika report using generator methods. Demonstrates `report().iter_lines()`, `report().iter_values()`, and `report().iter_dicts()` which yield rows sequentially from all parts without requiring explicit part iteration. This is useful for processing large reports efficiently row by row. Requires a `client` initialized and a `report` downloaded. ```python from tapi_yandex_metrika import YandexMetrikaLogsapi client = YandexMetrikaLogsapi(...) info = client.create().post(params=...) request_id = info["log_request"]["request_id"] report = client.download(requestId=request_id).get() print(report.columns) for row_as_line in report().iter_lines(): print(row_as_line) for row_as_values in report().iter_values(): print(row_as_values) for row_as_dict in report().iter_dicts(): print(row_as_dict) ``` -------------------------------- ### Automatically Downloading Yandex Metrika Report in Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/logsapi.md Configures the `tapi-yandex-metrika` client with `wait_report=True` to automatically poll the API and wait for a newly created report to be processed before attempting to download it. Demonstrates creating a report with specific parameters, downloading the report data once available, and accessing the data in raw format (`data`), column names (`columns`), and transformed formats (`to_values`, `to_lines`, `to_dicts`). Requires `ACCESS_TOKEN` and `COUNTER_ID` to be set. ```python from tapi_yandex_metrika import YandexMetrikaLogsapi ACCESS_TOKEN = "" COUNTER_ID = "" client = YandexMetrikaLogsapi( access_token=ACCESS_TOKEN, default_url_params={'counterId': COUNTER_ID}, # Download the report when it will be created wait_report=True, ) params={ "fields": "ym:s:date,ym:s:clientID,ym:s:dateTime,ym:s:startURL,ym:s:endURL", "source": "visits", "date1": "2019-01-01", "date2": "2019-01-01" } info = client.create().post(params=params) request_id = info["log_request"]["request_id"] report = client.download(requestId=request_id).get() print("Raw data") data = report.data print("Column names") print(report.columns) # Transform to values print(report().to_values()) # Transform to lines print(report().to_lines()) # Transform to dict print(report().to_dicts()) ``` -------------------------------- ### Fetching and Processing Yandex Metrika Report Data - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/stats.md This snippet demonstrates initializing the YandexMetrikaStats client, setting up report parameters (IDs, dates, metrics, dimensions, sort, language), making an API call to retrieve statistics, and accessing the report data in different formats: raw, column names, values list, dicts list, and column-oriented list. It requires an ACCESS_TOKEN and COUNTER_ID. ```python import datetime as dt from tapi_yandex_metrika import YandexMetrikaStats ACCESS_TOKEN = "" COUNTER_ID = "" client = YandexMetrikaStats(access_token=ACCESS_TOKEN) params = dict( ids=COUNTER_ID, date1="2020-10-01", date2=dt.date(2020,10,5), metrics="ym:s:visits", dimensions="ym:s:date", sort="ym:s:date", lang="en", # Other params -> https://yandex.ru/dev/metrika/doc/api2/api_v1/data.html ) report = client.stats().get(params=params) # Raw data print(report.data) print(report.columns) # ['ym:s:date', 'ym:s:visits'] report().to_values() #[ # ['2020-10-01', 14234.0], # ['2020-10-02', 12508.0], # ['2020-10-03', 12365.0], # ['2020-10-04', 14588.0], # ['2020-10-05', 14579.0] #] report().to_dicts() # Column data orient report().to_columns() #[ # ['2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04', '2020-10-05'], # [14234.0, 12508.0, 12365.0, 14588.0, 14579.0] #] ``` -------------------------------- ### Initializing Yandex Metrika Management Client - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/management.md Initializes the YandexMetrikaManagement client object. This object is the primary interface for interacting with the Yandex Metrika Management API resources. Requires an ACCESS_TOKEN and a default COUNTER_ID. Dependencies: tapi-yandex-metrika library. ```python from tapi_yandex_metrika import YandexMetrikaManagement ACCESS_TOKEN = "" COUNTER_ID = "" client = YandexMetrikaManagement( access_token=ACCESS_TOKEN, default_url_params={'counterId': COUNTER_ID} ) ``` -------------------------------- ### Iterating Through Yandex Metrika Report Parts and Rows in Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/logsapi.md Shows how to process a downloaded Yandex Metrika report by iterating through its constituent parts using `report().parts()`. Within each part, it demonstrates accessing raw data (`part.data`), transformed data (`part().to_values()`, `part().to_lines()`, `part().to_columns()`, `part().to_dicts()`), and iterating line by line in different formats (`part().lines()`, `part().values()`, `part().dicts()`). Requires a `client` initialized and a `report` downloaded. ```python from tapi_yandex_metrika import YandexMetrikaLogsapi client = YandexMetrikaLogsapi(...) info = client.create().post(params=...) request_id = info["log_request"]["request_id"] report = client.download(requestId=request_id).get() print(report.columns) # Iteration parts. for part in report().parts(): print(part.data) # raw data print(part().to_values()) print(part().to_lines()) print(part().to_columns()) # columns data orient print(part().to_dicts()) for part in report().parts(): # Iteration lines. for row_as_text in part().lines(): print(row_as_text) # Iteration values. for row_as_values in part().values(): print(row_as_values) # Iteration dicts. for row_as_dict in part().dicts(): print(row_as_dict) ``` -------------------------------- ### Iterating Through All Rows of a Yandex Metrika Report - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/stats.md This snippet demonstrates using simplified iteration methods to loop through all rows across all pages of a Yandex Metrika report without manually handling pagination. It shows iterating over rows directly as lists of values using `report().iter_values()` and as dictionaries using `report().iter_dicts()`. Requires a client and retrieved report object. ```python from tapi_yandex_metrika import YandexMetrikaStats client = YandexMetrikaStats(access_token=...) report = client.stats().get(params=...) for values in report().iter_values(): print(values) # ['2020-10-01', 14234.0] # ['2020-10-02', 12508.0] # ['2020-10-03', 12365.0] # ['2020-10-04', 14588.0] # ['2020-10-05', 14579.0] # ['2020-10-06', 12795.0] for row_as_dict in report().iter_dicts(): print(row_as_dict) ``` -------------------------------- ### Accessing Request Details - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/management.md Shows how to access detailed information about the HTTP response after making a request. This includes the full response object (`.response`), the response headers (`.response.headers`), and the HTTP status code (`.status_code`). ```python counters = client.counters().get() print(counters.response) print(counters.response.headers) print(counters.status_code) ``` -------------------------------- ### Iterating Through Yandex Metrika Report Pages - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/stats.md This snippet demonstrates how to iterate through all pages of a Yandex Metrika report using the `.pages()` method. For each page, it shows how to access the raw page data and convert it into different formats like lists of dictionaries (`to_dicts`), column-oriented lists (`to_columns`), and lists of values (`to_values`). It also illustrates iterating directly over rows within a page using `.values()` and `.dicts()`. Requires a client and retrieved report object. ```python from tapi_yandex_metrika import YandexMetrikaStats client = YandexMetrikaStats(access_token=...) report = client.stats().get(params=...) print("iteration report pages") for page in report().pages(): # Raw data. print(page.data) print(page().to_dicts()) print(page().to_columns()) print(page().to_values()) print("iteration report pages") for page in report().pages(): print("iteration rows as values") for row_as_values_of_page in page().values(): print(row_as_values_of_page) # ['2020-10-01', 14234.0] # ['2020-10-02', 12508.0] # ['2020-10-03', 12365.0] # ['2020-10-04', 14588.0] # ['2020-10-05', 14579.0] # ['2020-10-06', 12795.0] print("iteration rows as dict") for row_as_dict_of_page in page().dicts(): print(row_as_dict_of_page) ``` -------------------------------- ### Accessing Raw HTTP Response for Yandex Metrika Reports - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/stats.md This snippet shows how to access the underlying raw HTTP response object from both the main report object and individual page objects using the `.response` attribute. This allows access to details like the HTTP status code (`status_code`) and response headers (`headers`), which can be useful for debugging or inspecting API interactions. Requires a client and retrieved report object. ```python from tapi_yandex_metrika import YandexMetrikaStats client = YandexMetrikaStats(access_token=...) report = client.stats().get(params=...) print(report.response) print(report.response.status_code) print(report.response.headers) for page in report().pages(): print(page.response) print(page.response.status_code) print(page.response.headers) ``` -------------------------------- ### Limiting Iteration of Yandex Metrika Reports - Python Source: https://github.com/pavelmaksimov/tapi-yandex-metrika/blob/master/docs/stats.md This snippet demonstrates how to apply limits to iteration using the `max_pages` and `max_rows` parameters available on various iteration methods (`pages`, `values`, `dicts`, `iter_values`, `iter_dicts`). It shows limiting the number of pages iterated over and the number of rows retrieved per page, as well as limiting the total rows returned by the unified iterators. ```python from tapi_yandex_metrika import YandexMetrikaStats client = YandexMetrikaStats(access_token=...) report = client.stats().get(params=...) print("iteration report rows with limit") for page in report().pages(max_pages=2): for values in page().values(max_rows=2): print(values) # ['2020-10-01', 14234.0] # ['2020-10-02', 12508.0] # ['2020-10-06', 12795.0] print("Will iterate over all lines of all pages with limit") for values in report().iter_values(max_pages=2, max_rows=1): print(values) # ['2020-10-01', 14234.0] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.