Robo Robok
Robo Robok

Reputation: 22663

How to get user name from Google Analytics API?

I'm using Google Analytics API and SDK. After logging user in, I would like to display his name. Instead, when I call for user's ad accounts, the API returns just the e-mail without the display name. I'm using the https://www.googleapis.com/auth/analytics.edit scope.

Here's what I'm getting:

object(Google_Service_Analytics_Accounts)[86]
  protected 'collection_key' => string 'items' (length=5)
  protected 'internal_gapi_mappings' => ...
  protected 'itemsType' => string 'Google_Service_Analytics_Account' (length=32)
  protected 'itemsDataType' => string 'array' (length=5)
  public 'itemsPerPage' => int 1000
  public 'kind' => string 'analytics#accounts' (length=18)
  public 'nextLink' => null
  public 'previousLink' => null
  public 'startIndex' => int 1
  public 'totalResults' => int 2
  public 'username' => string '[email protected]' (length=12)
  protected 'modelData' => ...
  protected 'processed' => ...

How can I obtain user name? Do I need to use some other Google API?

Upvotes: 2

Views: 696

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

Unfortunately the Google Analytics API doesn't have access to information about a user beyond there email.

Even if you check Account users list it doesn't return the name of the user:

 "userRef": {
    "kind": "analytics#userRef",
    "id": "117200475532672775346",
    "email": "[email protected]"
   },

An option would be to add the profile scope which is part of the Google+ API. You will then be able to use people.get. Just add me as the user id, and it will return profile information for the person that is currently authenticated in:

 "objectType": "person",
 "id": "117200475532672775346",
 "displayName": "Linda Lawton",
 "name": {
  "familyName": "Lawton",
  "givenName": "Linda"
 },

Or I guess you could take the id from the Analytics account request. They appear to be the same id.

Upvotes: 3

Related Questions