Search code examples
pythonpython-3.xgoogle-analyticsgoogle-apigoogle-api-python-client

How to get page views for a batch of page names from google analytics API?


I have run the introduction code for Python, and it works. https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/installed-py

Inside the Analytics Web App I can find a page by Page Title and there I have metrics like pageView and avg. time on page.

Now, I would like to query the analytics API to get the same metrics for a batch of Page Titles. The use is for dataset creation, so I will just run this query a couple of times.

I find the documentation confusing, so any help that would point me in the right direction would help.

Thank you


Solution

  • You can query google analytics using below standard json as input to batchGet method

    {
      "reportRequests": [
    {
      "viewId": "",
      "dateRanges": [
        {
          "startDate": "",
          "endDate": ""
        }
      ],
      "metrics": [
        {
          "expression": ""
        }
      ],
      "dimensions": [
        {
          "name": ""
        }
      ],
      "dimensionFilterClauses": [
        {
          "filters": [
            {
              "expressions": []
            }
          ]
        }
      ]
    }
      ]
    }
    

    in expressions, you can add a list of queries you would like to give.

    This is basic query structure which will be using OR as FilterLogicalOperator But, In case you want to add more then specify operator with expressions

    Below is the list of specifications you can give: GA