Search code examples
pythongoogle-cloud-platformgoogle-admin-sdkgoogle-api-python-client

Google Workspace Reports API - Customer Usage Metrics - Get All Gmail Parameters


I am using Google Workspace Reports API to pull customer usage metrics for GMail via Python client. I would like to pull all parameters, but would prefer to not list out each of them individually (ie: service.customerUsageReports().get(date=temp, parameters='gmail:num_1day_active_users,gmail:num_30day_active_users,gmail:num_emails_received').execute())

I have tried parameters='gmail' and parameters='gmail:*' to no avail.

Is there a shorthand for pulling all customer usage metrics related to GMail, or do you have to pass each parameter individually?


Solution

  • I don't think you can. But if you like to have automated and have different parameters in multiple calls, you can do these steps:

    1. Store all possible parameters in an array.
    2. Filter the array using regex.
    3. Append all filtered elements into a string via comma
    4. Pass the string as the parameter

    Code:

    import re
    
    parameters = [
        'accounts:num_30day_logins',
        'accounts:num_7day_logins',
        'accounts:num_1day_logins',
        'accounts:num_disabled_accounts', 
        'accounts:apps_total_licenses',
        'accounts:apps_used_licenses',
        'accounts:num_users_2sv_enrolled',
        'gmail:num_emails_received',
        'gmail:num_inbound_spam_emails',
        'gmail:num_inbound_non_spam_emails',
        'gmail:num_inbound_delivered_emails',
        'gmail:num_emails_sent',
        'gmail:num_inbound_rejected_emails',
        'gmail:num_inbound_rerouted_emails',
        'gplus:num_video_calls',
        'gplus:num_video_conferences',
        'gplus:num_video_conferences_cfm',
        'gplus:total_video_call_minutes',
        'docs:num_docs']
    
    separator = ','
    r_gmail = re.compile("gmail.*")
    r_accounts = re.compile("accounts.*")
    gmail_list = list(filter(r_gmail.match, parameters))
    accounts_list = list(filter(r_accounts.match, parameters))
    gmail_parameters = separator.join(gmail_list) 
    accounts_parameters = separator.join(accounts_list) 
    
    # contains gmail parameters
    print(gmail_parameters)
    # contains accounts parameters
    print(accounts_parameters)
    

    Hopefully you get the idea of the above code. This will be helpful when your script gets large and lots of parameters come and go.

    This way, you don't need to maintain numerous long lists of parameters. Just one and you only need to create a regex for it to be filtered the way you like it.

    Output:

    output