Scraping Real Time Visitors from Google Analytics

前端 未结 4 1149
梦谈多话
梦谈多话 2020-12-05 02:44

I have a lot of sites and want to build a dashboard showing the number of real time visitors on each of them on a single page. (would anyone else want this?) Right now the o

4条回答
  •  孤街浪徒
    2020-12-05 03:44

    I needed/wanted realtime data for personal use so I reverse-engineered their system a little bit.

    Instead of binding to /bind I get data from /getData (no pun intended).

    At /getData the minimum request is apparently: https://www.google.com/analytics/realtime/realtime/getData?pageId&key={{propertyID}}&q=t:0|:1

    Here's a short explanation of the possible query parameters and syntax, please remember that these are all guesses and I don't know all of them:

    Query Syntax: pageId&key=propertyID&q=dataType:dimensions|:page|:limit:filters

    Values:

    pageID: Required but seems to only be used for internal analytics.
    
    propertyID: a{{accountID}}w{{webPropertyID}}p{{profileID}}, as specified at the Documentation link below. You can also find this in the URL of all analytics pages in the UI.
    
    
    dataType:
        t: Current data
        ot: Overtime/Past
        c: Unknown, returns only a "count" value
    
    
    dimensions (| separated or alone), most values are only applicable for t:
        1:  Country
        2:  City
        3:  Location code?
        4:  Latitude
        5:  Longitude
        6:  Traffic source type (Social, Referral, etc.)
        7:  Source
        8:  ?? Returns (not set)
        9:  Another location code? longer.
        10: Page URL
        11: Visitor Type (new/returning)
        12: ?? Returns (not set)
        13: ?? Returns (not set)
        14: Medium
        15: ?? Returns "1"
    
    page:
        At first this seems to work for pagination but after further analysis it looks like it's also used to specify which of the 6 pages (Overview, Locations, Traffic Sources, Content, Events and Conversions) to return data for.
    
        For some reason 0 returns an impossibly high metrictotal
    
    limit: Result limit per page, maximum of 50
    
    filters:
        Syntax is as specified at the Documentation 2 link below except the OR is specified using | instead of a comma.6==CUSTOM;1==United%20States
    


    You can also combine multiple queries in one request by comma separating them (i.e. q=t:1|2|:1|:10,t:6|:1|:10).

    Following the above "documentation", if you wanted to build a query that requests the page URL and city of the top 10 active visitors with a traffic source type of CUSTOM located in the US you would use this URL: https://www.google.com/analytics/realtime/realtime/getData?key={{propertyID}}&pageId&q=t:10|2|:1|:10:6==CUSTOM;1==United%20States


    Documentation

    Documentation 2


    I hope that my answer is readable and (although it's a little late) sufficiently answers your question and helps others in the future.

提交回复
热议问题