Search code examples
google-analyticsgoogle-analytics-api

Getting the base URL for a property via Google Analytics API?


I'm working with the Google Analytics API, and am pulling a lot of data from it successfully. The accounts feed (https://www.google.com/analytics/feeds/accounts/default) returns a list of web properties connected to the account you're authorized for, in the following format (most actual values replaced, not sure how sensitive any of it is):

<entry gd:etag="W/&quot;<charsblahblahblah>.&quot;" gd:kind="analytics#account">
    <id>http://www.google.com/analytics/feeds/accounts/ga:########</id>
    <updated>2011-08-04T12:01:33.467-07:00</updated>
    <title>www.afifthofnothing.com</title>
    <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
    <dxp:property name="ga:accountId" value="#####"/>
    <dxp:property name="ga:accountName" value="My Sites"/>
    <dxp:property name="ga:profileId" value="######"/>
    <dxp:property name="ga:webPropertyId" value="UA-#####-#"/>
    <dxp:property name="ga:currency" value="USD"/>
    <dxp:property name="ga:timezone" value="America/Los_Angeles"/>
    <dxp:tableId>ga:#######</dxp:tableId>
</entry>

I've previously been using the <title> element as the base URL for the property, which usually works because that's the default title that Google Analytics assigns to the property. But if a user edits this (I'm querying my end-users' accounts, so I don't have control there) to something like "A Fifth Of Nothing", then the Analytics API will dutifully return that as the title, leaving me with no way to find the base URL.

I've scoured the Analytics API documentation and googled around, searched SO, but don't see any way to get the base URL of a web property from Google Analytics. I've tried querying the id field via the OAuth Playground for more info, and it just gives me more errors, saying the last part has to be /default.

Is there a way to get the base URL for a user's properties from the data API? It seems like this would be something pretty basic. If not, I'll have to resort to trying the title, and if it's not URL-like just have the user enter it themselves, which is not very user-friendly, but is the only thing I can come up with at this point.


Solution

  • The strategy to use is to do a query against the ga:hostname dimension for ga:visits and sort descending. You typically can use this to reveal the host/base url.

    I don't think there is such a property for the "base url" because technically you can put the google tracking code on multiple websites. And in this instance you would have multiple base url.