0

I've just started learning a bit of R in order to pull and analyse data from Google Analytics. What I'm struggling with is querying the management API to pull certain account data.

My desire is to be able to pull the definition of a segment or the definition of all my segments. That is to be able to find out how the segment is built e.g. page url contains 'questions'.

I'm using RGoogleAnalyics package. I've found this source code (https://r-google-analytics.googlecode.com/svn-history/r32/trunk/src/RGoogleAnalytics/R/Configuration.R) but not quite sure how to interpret that into script. I've tried experimenting but not getting any success - variety of errors around not being able to find functions such as Validatetoken or RetrieveConfigurationData

Any help as to how I need to format my query to get this would be really appreciated. Thanks

Fox87
  • 127
  • 2
  • 2
  • 10

1 Answers1

0

Fox, I am personally using RGA package and it works seamlessly.

See the segments documentation on Google Analytics Developers Site, the code is as follows:

ga$getData(ids, batch = TRUE, walk = TRUE, 
           start.date, end.date, 
           metrics = "ga:visits,ga:transactions", 
           dimensions="ga:keyword",
           filter="ga:country==Denmark;ga:medium==organic",
           segment="dynamic::ga:medium%3D%3Dreferral")

As a rule of thumb for complex segment I would advise creating them in GA interface and then simply referring them with segment ID.

Also be aware that you cannot reference more than one segment (unlike in GA interface), so for everyt segment you need to analyze, you need to place API request.

Petr Havlik
  • 3,307
  • 1
  • 19
  • 17
  • Thanks Petr. What I really need to know is if I have created a segment in GA, how can I find out using R what that segment is? E.g. I want to say to R show me how this segment is built where segment id is 7. And then it returns something like 'ga:medium%3D%3Dreferral'. Is that possible do you know? – Fox87 Jan 17 '14 at 13:25
  • Unfortunately, that is not possible. All you can access with API is the segment ID... Query Explorer might come handy for this purpose as well: http://ga-dev-tools.appspot.com/explorer/ – Petr Havlik Jan 17 '14 at 15:05
  • @PetrHavlik I seem to be having issues pulling data from a custom segment. Say for example I have the named the segment 'Funnel 1 - Customers', How would this be an input? – A.Kot Nov 02 '16 at 18:32
  • A.Kot, I suggest using this tool from Google: https://ga-dev-tools.appspot.com/query-explorer/. Once you authorize it, you should see the list of your custom segments and when you select any of them, it will give you the segment ID which you should use in the API request. – Petr Havlik Nov 03 '16 at 08:21