Get top Google Analytics items sorted by last X days metric

eg. I want to get the three most visited pages in the last 30 days, sorted by number of sessions.

So, for each day, I want three items ga:pagePath

sorted for each day to be ga:sessions

. Is it possible in one request?

This request should return 90 items, 3 ga:pagePath

items per day.

As another example, would you get the three fastest loading pages (ga: pageLoadTime) instead of most visited each day? It does not require ASC sorting, but DESC regarding the sorting metric.

I can use API v4 if needed.

+3


source to share


1 answer


1) So for each individual day, I want the three top ga: pagePath elements to be sorted for each day using ga: sessions. Is it possible in one request?

Yes it is possible. This can be done through unique page views. Google states:Unique Pageviews is the number of sessions during which the specified page was viewed at least once. A unique pageview is counted for each page URL + page Title combination.

The obvious caveat is the URL + page combination, but this can work in your favor depending on the application.

In Google Analytics:

Go to Behavior> Site Content> All Pages. Sort by unique page views.

In API GAv4

{
  "reportRequests": [
    {
      "viewId": "VIEW_ID",
      "dateRanges": [
        {
          "startDate": "30daysAgo",
          "endDate": "yesterday"
        }
      ],
      "metrics": [
        {
          "expression": "ga:uniquePageViews"
        }
      ],
      "dimensions": [
        {
          "name": "ga:pagePath"
        }
      ],
      "pageSize": 3,
      "orderBys": [
        {
          "fieldName": "ga:uniquePageViews",
          "sortOrder": "DESCENDING"
        }
      ]
    }
  ]
}

      

2) Is it also possible to get the three fastest loading pages (ga: pageLoadTime) instead of most visited each day? This does not require ASC sorting, but DESC regarding the sorting metric.

Yes, that is also possible. However, you probably want to use ga:avgPageLoadTime

as ga:pageLoadTime

is the total time (of all pageviews).

In Google Analytics:

Go to Behavior> Site Speed> Page Timing. Modify the numeric column in the Avg tool. Page load time (sec) and sorting by this column.



In API GAv4

{
  "reportRequests": [
    {
      "viewId": "VIEWID",
      "dateRanges": [
        {
          "startDate": "30daysAgo",
          "endDate": "yesterday"
        }
      ],
      "metrics": [
        {
          "expression": "ga:uniquePageViews"
        }
      ],
      "dimensions": [
        {
          "name": "ga:pagePath"
        }
      ],
      "pageSize": 3,
      "orderBys": [
        {
          "fieldName": "ga:avgPageLoadTime",
          "sortOrder": "ASCENDING"
        }
      ]
    }
  ]
}

      

However, this report may not be very useful as it will pick up emissions first and not pages of interest. I would combine this call with another call to fetch, 1) to the top Russian pages using unique pageviews, and 2) use that as a filter to call the page load time.

For example (in R, using googleAnalyticsR):

1)
#retrieve the top 100 pages (ranked via unique pageviews)
test <- google_analytics_4(VIEW_ID, 
  date_range = c("30daysAgo", "yesterday"), 
  metrics = "uniquePageviews",
  dimensions = "pagePath", 
  max=100, #this is the top n pages
  order = order_type("uniquePageviews", sort_order=c("DESCENDING"),orderType = c("VALUE")))

2)
#call the top 3 pages via avgPageLoadTime
test2 <- google_analytics_4(VIEW_ID, 
  date_range = c("30daysAgo", "yesterday"), 
  metrics = "avgPageLoadTime",   
#But only for the top 100 pages (ranked via unique pageviews).
#filterExpression = uniquePageViews is greater then the unique pageviews 
#of the 100th ranked page.
  filtersExpression=paste("ga:uniquePageViews>",tail(test$uniquePageviews, 1), sep=""),
  dimensions = "pagePath", 
  max=3, 
  order = order_type("avgPageLoadTime", sort_order=c("ASCENDING"),orderType = c("VALUE")))

      

Update:

What you are trying to achieve is not possible in a single request.

The problem is that the order of the results is responsive to the day you are trying to query.

The pivot table won't work, even though you can turn the page by day and have 30 columns, it will show you daily traffic to the top three pages instead of the top three pages per day.

To get the results you are looking for, you will need to run a loop for the last day x, naming the top 3 results for the digit of interest.

+2


source







All Articles