Package: analytix
A simple yet powerful SDK for the YouTube Analytics API.FeaturesPythonic syntax lets you feel right at homeDynamic error handling saves hours of troubleshooting and makes sure only valid requests count toward your API quotaA clever interface allows you to make multiple requests across multiple sessions without reauthorisingExtra support enables you to export reports in a variety of filetypes and to a number of DataFrame formatsEasy enough for beginners, but powerful enough for advanced usersInstallationInstalling analytixTo install the latest stable version of analytix, use the following command:pipinstallanalytixYou can also install the latest development version using the following command:pipinstallgit+https://github.com/parafoxia/analytixYou may need to prefix these commands with a call to the Python interpreter depending on your OS and Python configuration.DependenciesBelow is a list of analytix's dependencies.
Note that the minimum version assumes you're using CPython 3.8.
The latest versions of each library are always supported.NameMin. versionRequired?Usageurllib31.10.0YesMaking HTTP requestsjwt1.2.0NoDecoding JWT ID tokens (from v5.1)openpyxl3.0.0NoExporting report data to Excel spreadsheetspandas1.4.0NoExporting report data to pandas DataFramespolars0.15.17NoExporting report data to Polars DataFramespyarrow5.0.0NoExporting report data to Apache Arrow tables and file formatsOAuth authenticationAll requests to the YouTube Analytics API need to be authorised through OAuth 2.
In order to do this, you will need a Google Developers project with the YouTube Analytics API enabled.
You can find instructions on how to do that in theAPI setup guide.Once a project is set up, analytix handles authorisation — including token refreshing — for you.More details regarding how and when refresh tokens expire can be found on theGoogle Identity documentation.UsageRetrieving reportsThe following example creates a CSV file containing basic info for the 10 most viewed videos, from most to least viewed, in the US in 2022:fromdatetimeimportdatefromanalytiximportClientclient=Client("secrets.json")report=client.fetch_report(dimensions=("video",),filters={"country":"US"},metrics=("estimatedMinutesWatched","views","likes","comments"),sort_options=("-estimatedMinutesWatched",),start_date=date(2022,1,1),end_date=date(2022,12,31),max_results=10,)report.to_csv("analytics.csv")If you want to analyse this data using additional tools such aspandas, you can directly export the report as a DataFrame or table using theto_pandas(),to_arrow(), andto_polars()methods of the report instance.
You can also save the report as a.tsv,.json,.xlsx,.parquet, or.featherfile.There are more examples in theGitHub repository.Fetching group informationYou can also fetch groups and group items:fromanalytiximportClient# You can also use the client as context manager!withClient("secrets.json")asclient:groups=client.fetch_groups()group_items=client.fetch_group_items(groups[0].id)LoggingIf you want to see what analytix is doing, you can enable the packaged logger:importanalytixanalytix.enable_logging()This defaults to showing all log messages of level INFO and above.
To show more (or less) messages, pass a logging level as an argument.CompatibilityCPython versions 3.8 through 3.12 and PyPy versions 3.8 through 3.10 are officially supported*.
CPython 3.13-dev is provisionally supported*.
Windows, MacOS, and Linux are all supported.*For base analytix functionality; support cannot be guaranteed for functionality requiring external libraries.ContributingContributions are very much welcome! To get started:Familiarise yourself with thecode of conductHave a look at thecontributing guideLicenseThe analytix module for Python is licensed under theBSD 3-Clause License.