Google Analytics API - Get page view information for specific URLs
Asked Answered
B

4

73

I am looking to implement a page view tracking system on one of my websites.

The website is a directory where suppliers can advertise their services. Suppliers have a unique profile page on the site, e.g mysite.com/directory/abc-profile.html

The idea is that suppliers can log in to their account area on the site and view stats on how many people are visiting their profile. Ideally I want to display this as :

Total views | Total today | This week | This month | This year

It does not matter if the data is not completely up to date.

I already have Google Analytics tracking code installed on the site. Is it possible to use the Google Analytics API to retrieve this data? If so, what kind of query do I need to make? I had a look on the documentation but could not figure whether the functions allow this or not.

I am using PHP and MySQL on the server.

Bronny answered 20/10, 2013 at 23:16 Comment(1)
I need same to be doneTaiwan
G
77

Yes - you will need to use the Google Analytics API for this. I would suggest checking out the Query Explorer to get a feel for the queries you will need to create.

You will require numerous queries to get all the data you need (adjusting the starting date): - Total Views - This Year - This Month - This Week (i.e. last 7 days - from which you could also get Total Today).

Here is an example query:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:1234456789&dimensions=ga:pagePath&metrics=ga:pageviews&filters=ga:pagePath==/about-us.html&start-date=2013-10-15&end-date=2013-10-29&max-results=50

Alternatively, you might want to consider www.embeddedanalytics.com (disclosure - I work with them). We have a service/platform that allows website owners to embed GA based charts/statistics without having to learn the GA API. We have a CMS version which will do exactly what you need (where you script the call to pass the page path). We have done something like this with a number of podcast sharing sites.

Gord answered 29/10, 2013 at 19:59 Comment(4)
It says login required even if I am logged in. WHY?Colloquium
@Colloquium there's a difference between being logged in and being authorized to let a third-party app access your data. You have to both be logged in and authorize this app.Elenoraelenore
For Reporting API V4 see the answer below.Alongside
This query explorer is life saver thanks for sharing.Understudy
A
69

Google suggests using Reporting API V4 now. The accepted answer uses V3.

Here is a V4 request example:

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet?key={YOUR_API_KEY}
{
 "reportRequests": [
  {
   "viewId": "YOUR_VIEW_ID",
   "dimensions": [
    {
     "name": "ga:pagePath"
    }
   ],
   "metrics": [
    {
     "expression": "ga:pageviews"
    }
   ],
   "dimensionFilterClauses": [
    {
     "filters": [
      {
       "operator": "EXACT",
       "dimensionName": "ga:pagePath",
       "expressions": [
        "/your-path"
       ]
      }
     ]
    }
   ],
   "dateRanges": [
    {
     "startDate": "2009-12-31",
     "endDate": "2016-09-28"
    }
   ]
  }
 ]
}

where
YOUR_API_KEY - for auth related things follow this page
YOUR_VIEW_ID - you can use the Account Explorer to find a View ID. (or Admin -> View -> View Settings -> View ID).

For more documentation details and a "Try it!" console follow this page.

Alongside answered 28/9, 2016 at 13:31 Comment(11)
This has to be done server side right? because it is asking for access tokenHypomania
I am getting 401 unauthorized even with API KEY.Waligore
It's quite complicated to authorize correctly as I remember.Alongside
is it possible to add multiple pagePaths?Minni
@TylerZika I don't remember, but I don't add in the code. Look at docs or experiment.Alongside
can we use regular expression in 'dimensionFilterClauses'?Gahan
How do we get individual pages from this API?Injurious
I am getting "{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token,"Distiller
looks like it's outdated answer. there's no view_id anymoreAmaleta
@Amaleta I opened the Account Explorer link, signed in and immediately found all my View IDs. It's in the table, you can copy your View IDs from the View column on ga-dev-tools.web.app/account-explorer.Alongside
I'm on latest GA v4 version and I think view ids are available for older UA version https://mcmap.net/q/275564/-what-is-a-viewid-in-google-analyticsAmaleta
S
7

Thought I would provide an updated version as the others are showing for V3 and V4 - for anyone using the latest API for Google Analytics Data API (GA4). This is just grabbing the total users, over a date range for a specific path.

Note that the POST is hitting the v1 beta address as this was the latest at the time.

POST https://analyticsdata.googleapis.com/v1beta/properties/PROPERTY_ID:runReport
{
  "dateRanges": [
    {
      "startDate": "YYYY-MM-DD",
      "endDate": "YYYY-MM-DD"
    }
  ],
  "dimensions": [
    {
      "name": "pagePath"
    }
  ],
  "dimensionFilter": {
    "filter": {
      "fieldName": "pagePath",
      "stringFilter": {
        "matchType": "CONTAINS",
        "value": "/YOUR/PATH"
      }
    }
  },
  "metrics": [
    {
      "name": "totalUsers"
    }
  ]
}
Soothsay answered 6/9, 2022 at 21:56 Comment(0)
F
1

You should be able to add a filter on the landing page. I am assuming that each user's site has its own start page. This returns only the data for that user. If you want the code on how to do this I suggest you google: Google analytics core reporting API PHP tutorial

Another idea would be to let the user add there Google Analytics account to there profile. Then you can output the google analytics code onto there page. Then they can track there own google analytics data and you won't need to deal with any of it.

Freehand answered 21/10, 2013 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.