client
Client interfaces for analytix.
There are two clients to choose from:
- The base client (
BaseClient
) - The scripting client (
Client
)
The scripting client is the simpler of the two to use and is designed for use in scripts. Client authorisation and shard management is handled for you, allowing you to focus on the data you wish to fetch.
The base client needs to be subclassed, and is designed for use in web applications. It needs to be told how to authorise itself to best suit your workflow, and requires you to create and destroy shards yourself.
You may wish to use the base client if:
- you're running your application on a server
- you want control over how your applications is authorised
- you want to store tokens in a database
- you need access to JWT ID tokens
BaseClient
Bases: RequestMixin
A base client designed to be subclassed for use in web applications.
This client provides an interface for retrieving reports and group data from the YouTube Analytics API, but requires you to implement your own authorisation routine. It also requires you to manually create and manage analytix "shards", though utilities are available to help with that.
This will work as a context manager.
New in version 5.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secrets_file |
PathLike
|
The path to your secrets file. |
required |
scopes |
Scopes
|
The scopes to allow in requests. This is used to control whether or not to allow access to monetary data, as well as whether to fetch ID tokens. If this is not provided, neither monetary data nor ID tokens will be accessible. |
READONLY
|
Raises:
Type | Description |
---|---|
AuthorisationError
|
Neither the READONLY nor MONETARY_READONLY scopes were provided. |
Examples:
>>> from analytix import BaseClient
>>> class CustomClient(BaseClient):
... pass
...
>>> client = CustomClient("secrets.json")
Providing custom scopes.
>>> from analytix import BaseClient, Scopes
>>> class CustomClient(BaseClient):
... pass
...
>>> client = CustomClient("secrets.json", scopes=Scopes.ALL)
Providing JWT scopes.
>>> from analytix import BaseClient, Scopes
>>> class CustomClient(BaseClient):
... pass
...
>>> client = CustomClient(
... "secrets.json",
... scopes=Scopes.READONLY | Scopes.ALL_JWT,
... )
Source code in analytix/client.py
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
|
authorise
abstractmethod
An abstract method used to authorise the client.
The BaseClient
requires you to overload this method when
subclassing to suit your application's needs. Your
implementation of this method must return a Tokens
object.
Returns:
Type | Description |
---|---|
Tokens
|
Your tokens. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
You called this method without overloading it. |
See Also
You may find the analytix.auth.auth_uri
and .token_uri
functions helpful when writing your custom implementation.
Source code in analytix/client.py
decode_id_token
A helper method to decode an ID token.
ID tokens are returned from the YouTube Analytics API as a JWT, which is a secure way to transfer encrypted JSON objects. This method decrypts and decodes the JWT and returns the stored information.
You will only receive an ID token if you specifically tell the client to fetch one.
New in version 5.1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token |
str
|
Your ID token. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The decoded ID token. |
Raises:
Type | Description |
---|---|
MissingOptionalComponents
|
python-jwt is not installed. |
IdTokenError
|
Your ID token could not be decoded. This may be raised alongside other errors. |
Notes
This requires jwt
to be installed to use, which is an optional
dependency.
Examples:
>>> client = BaseClient("secrets.json")
>>> tokens = client.authorise() # Overloaded using your impl.
>>> if tokens.id_token:
... id_token = client.decode_id_token(tokens.id_token)
Source code in analytix/client.py
refresh_access_token
Refresh your access token.
Changed in version 5.0
This is now handled by the client rather than by individual shards.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokens |
Tokens
|
Your tokens. |
required |
Returns:
Type | Description |
---|---|
Optional[Tokens]
|
Your refreshed tokens, or |
Notes
While this method should always be sufficient to refresh your access token, the default implementation does not save new tokens anywhere. If this is something you need, you will need to extend this method to accommodate that.
Examples:
Source code in analytix/client.py
scopes_are_sufficient
A helper method to check whether your stored scopes are sufficient.
This cross-checks the scopes you provided the client with the scopes your tokens are authorised with and determines whether your tokens provide enough access.
This is not an equality check; if your tokens are authorised
with all scopes, but you only passed the READONLY scope to the
client, this will return True
.
New in version 5.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scopes |
str
|
Your stored scopes. These are the scopes in your tokens, not the ones you passed to the client. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the scopes are sufficient or not. If they're not, you'll need to reauthorise. |
Examples:
Source code in analytix/client.py
shard
A context manager for creating shards.
You can think of shards as mini-clients, each able to make requests using their own tokens. This allows you to accommodate the needs of multiple users, or even allow a single user to make multiple requests, without having to call your authorisation routine multiple times.
Generally, shards should only live for a single request or a batch of related requests.
Changed in version 5.0
You can now provide custom scopes for shards.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokens |
Tokens
|
Your tokens. |
required |
scopes |
Optional[Scopes]
|
The scopes to allow in requests. If this is not provided, the shard will inherit the client's scopes. |
None
|
Yields:
Type | Description |
---|---|
Shard
|
A new shard. It will be destroyed upon exiting the context manager. |
Examples:
Providing custom scopes.
>>> from analytix import Scopes
>>> # Other custom logic.
>>> tokens = client.authorise()
>>> with client.shard(tokens, scopes=Scopes.ALL) as shard:
... shard.fetch_groups()
Source code in analytix/client.py
token_is_valid
A helper method to check whether your access token is valid.
The default implementation makes a call to Google's OAuth2 API to determine the token's validity.
New in version 5.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token |
str
|
Your access token. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the token is valid or not. If it isn't, it needs refreshing. |
Examples:
Source code in analytix/client.py
Client
Bases: BaseClient
A fully-functional client designed for use in scripts.
Unlike the base client, this client is capable of authorising itself and provides helper methods to abstract shard management away from you.
This will work as a context manager.
Changed in version 5.0
- You should now provide a tokens file rather than a tokens directory
auto_open_browser
is now set based on your OS by default- Monetary data is now no longer accessible by default
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secrets_file |
PathLike
|
The path to your secrets file. |
required |
scopes |
Scopes
|
The scopes to allow in requests. This is used to control whether or not to allow access to monetary data. If this is not provided, monetary data will not be accessible. |
READONLY
|
tokens_file |
PathLike
|
The path to save your tokens to. This must be a JSON file, but does not need to exist. If this is not provided, your tokens will be saved to a file called "tokens.json" in your current working directory. |
'tokens.json'
|
ws_port |
int
|
The port the client's webserver will use during authorisation. |
8080
|
auto_open_browser |
Optional[bool]
|
Whether to automatically open a new browser tab when
authorising. If this is |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
|
Source code in analytix/client.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 |
|
authorise
Authorise the client.
Client methods authorise the client for you, so you don't need to call this manually when using those. If you plan to make multiple requests in a row using the same client, it's better to call this manually and create a shard with the generated tokens to avoid authorising the client multiple times.
Changed in version 5.0
- You can no longer pass a filename to load a specific set of tokens; if you wish to change which channel is authorised, you should either utilise shards or forcibly reauthorise the client
- You can now forcibly refresh access tokens using this method
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force |
bool
|
Whether to forcibly reauthorise the client even if your tokens are still valid. |
False
|
force_refresh |
bool
|
Whether to forcibly refresh your access token even if the token is still valid. |
False
|
Returns:
Type | Description |
---|---|
Tokens
|
Your tokens. |
Raises:
Type | Description |
---|---|
RuntimeError
|
The client attempted to open a new browser tab, but failed. |
AuthorisationError
|
Something went wrong during authorisation. |
Notes
This method always does the minimum possible work necessary to authorise the client unless forced to do otherwise. This means that stored tokens will be prioritised, and the full auth flow will only trigger when necessary. Token refreshing is handled automatically.
Examples:
Source code in analytix/client.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
|
fetch_group_items
Authorise the client and fetch a list of all items within a group.
Changed in version 5.0
You can now pass a series of keyword arguments to be passed
on to the authorise
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_id |
str
|
The ID of the group to fetch items for. |
required |
**kwargs |
Any
|
Additional keyword arguments to be passed to the |
{}
|
Returns:
Type | Description |
---|---|
GroupItemList
|
An object containing the list of group items and the next page token. |
Raises:
Type | Description |
---|---|
BadRequest
|
Your request was invalid. |
Unauthorised
|
Your access token is invalid. |
Forbidden
|
You tried to access data you're not allowed to access. If your channel is not partnered, this is raised when you try to access monetary data. |
RuntimeError
|
The client attempted to open a new browser tab, but failed. |
AuthorisationError
|
Something went wrong during authorisation. |
Source code in analytix/client.py
fetch_groups
fetch_groups(*, ids: Optional[Collection[str]] = None, next_page_token: Optional[str] = None, **kwargs: Any) -> GroupList
Authorise the client and fetch a list of analytics groups.
Changed in version 5.0
You can now pass a series of keyword arguments to be passed
on to the authorise
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
Optional[Collection[str]]
|
The IDs of groups you want to fetch. If none are provided, all your groups will be fetched. |
None
|
next_page_token |
Optional[str]
|
If you need to make multiple requests, you can pass this to load a specific page. To check if you've arrived back at the first page, check the next page token from the request and compare it to the next page token from the first page. |
None
|
**kwargs |
Any
|
Additional keyword arguments to be passed to the |
{}
|
Returns:
Type | Description |
---|---|
GroupList
|
An object containing the list of your groups and the next page token. |
Raises:
Type | Description |
---|---|
BadRequest
|
Your request was invalid. |
Unauthorised
|
Your access token is invalid. |
Forbidden
|
You tried to access data you're not allowed to access. If your channel is not partnered, this is raised when you try to access monetary data. |
RuntimeError
|
The client attempted to open a new browser tab, but failed. |
AuthorisationError
|
Something went wrong during authorisation. |
Source code in analytix/client.py
fetch_report
fetch_report(*, dimensions: Optional[Collection[str]] = None, filters: Optional[Dict[str, str]] = None, metrics: Optional[Collection[str]] = None, start_date: Optional[dt.date] = None, end_date: Optional[dt.date] = None, sort_options: Optional[Collection[str]] = None, max_results: int = 0, currency: str = 'USD', start_index: int = 1, include_historical_data: bool = False, **kwargs: Any) -> Report
Authorise the client and fetch an analytics report.
Changed in version 5.0
This used to be retrieve_report
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dimensions |
Optional[Collection[str]]
|
The dimensions to use within the request. |
None
|
filters |
Optional[Dict[str, str]]
|
The filters to use within the request. |
None
|
metrics |
Optional[Collection[str]]
|
The metrics to use within the request. If none are provided, all supported metrics are used. |
None
|
sort_options |
Optional[Collection[str]]
|
The sort options to use within the request. |
None
|
Returns:
Type | Description |
---|---|
Report
|
The generated report. |
Other Parameters:
Name | Type | Description |
---|---|---|
start_date |
Optional[date]
|
The date in which data should be pulled from. If this is
not provided, this is set to 28 days before |
end_date |
Optional[date]
|
The date in which data should be pulled to. If this is not provided, this is set to the current date. |
max_results |
int
|
The maximum number of results the report should include. If
this is |
currency |
str
|
The currency revenue data should be represented using. This should be an ISO 4217 currency code. |
start_index |
int
|
The first row in the report to include. This is one-indexed.
If this is |
include_historical_data |
bool
|
Whether to include data from before the current channel owner assumed control of the channel. You only need to worry about this is the current channel owner did not create the channel. |
**kwargs |
Any
|
Additional keyword arguments to be passed to the |
Raises:
Type | Description |
---|---|
InvalidRequest
|
Your request was invalid. |
BadRequest
|
Your request was invalid, but it was not caught by analytix's verification systems. |
Unauthorised
|
Your access token is invalid. |
Forbidden
|
You tried to access data you're not allowed to access. If your channel is not partnered, this is raised when you try to access monetary data. |
RuntimeError
|
The client attempted to open a new browser tab, but failed. |
AuthorisationError
|
Something went wrong during authorisation. |
Warnings
- If your channel is not partnered, attempting to access
monetary data will result in a
Forbidden
error. Ensure your scopes are set up correctly before calling this method. - The "isCurated" filter stopped working on 30 Jun 2024. See the guide on new playlist reports for information on how to migrate.
See Also
You can learn more about dimensions, filters, metrics, and sort options by reading the detailed guides.
Examples:
Fetching daily analytics data for 2022.
>>> import datetime
>>> shard.fetch_report(
... dimensions=("day",),
... start_date=datetime.date(2022, 1, 1),
... end_date=datetime.date(2022, 12, 31),
... )
Fetching 10 most watched videos over last 28 days.
>>> shard.fetch_report(
... dimensions=("video",),
... metrics=("estimatedMinutesWatched", "views"),
... sort_options=("-estimatedMinutesWatched",),
... max_results=10,
... )
Source code in analytix/client.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
|
refresh_access_token
Refresh your access token.
New in version 5.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokens |
Tokens
|
Your tokens. |
required |
force |
bool
|
Whether to forcibly refresh your access token, even if the token is still valid. |
False
|
Returns:
Type | Description |
---|---|
Optional[Tokens]
|
Your refreshed tokens, or |
Notes
This method should never need to be called, as the authorise
method will call it automatically when necessary.
Examples: