document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #### Connecting to Google Analytics API via R
  2. #### Uses OAuth 2.0
  3. #### https://developers.google.com/analytics/devguides/reporting/core/v3/ for documentation
  4.  
  5. # Install devtools package & rga - This is only done one time
  6. install.packages("devtools")
  7. library(devtools)
  8. install_github("rga", "skardhamar")
  9.  
  10.  
  11. # Load rga package - requires bitops, RCurl, rjson
  12. # Load lubridate to handle dates
  13. library(rga)
  14. library(lubridate)
  15.  
  16. # Authenticating to GA API. Go to https://code.google.com/apis/console/ and create
  17. # an API application.  Don\'t need to worry about the client id and shared secret for
  18. # this R code, it is not needed
  19.  
  20. # If file listed in "where" location doesn\'t exist, browser window will open.
  21. # Allow access, copy code into R console where prompted
  22. # Once file located in "where" directory created, you will have continous access to
  23. # API without needing to do browser authentication
  24. rga.open(instance = "ga", where = "~/Documents/R/ga-api")
  25.  
  26.  
  27. # Get (not provided) Search results.  Replace XXXXXXXX with your profile ID from GA
  28. visits_notprovided.df <- ga$getData(XXXXXXXX,
  29.                                   start.date = "2011-01-01",
  30.                                   end.date = "2013-01-10",
  31.                                   metrics = "ga:visits",
  32.                                   filters = "ga:keyword==(not provided);ga:source==google;ga:medium==organic",
  33.                                   dimensions = "ga:date",
  34.                                   max = 1500,
  35.                                   sort = "ga:date")
  36.  
  37. names(visits_notprovided.df)<- c("hit_date", "np_visits")
  38.  
  39. # Get sum of all Google Organic Search results.  Replace XXXXXXXX with your profile ID from GA
  40. visits_orgsearch.df <- ga$getData(XXXXXXXX,
  41.                                     start.date = "2011-01-01",
  42.                                     end.date = "2013-01-10",
  43.                                     metrics = "ga:visits",
  44.                                     filters = "ga:source==google;ga:medium==organic",
  45.                                     dimensions = "ga:date",
  46.                                     max = 1500,
  47.                                     sort = "ga:date")
  48.  
  49. names(visits_orgsearch.df)<- c("hit_date", "total_visits")
  50.  
  51. # Merge files, create metrics, limit dataset to just days when tags firing
  52. merged.df <- merge(visits_notprovided.df, visits_orgsearch.df, all=TRUE)
  53. merged.df$search_term_provided <- merged.df$total_visits - merged.df$np_visits
  54. merged.df$pct_np <- merged.df$np_visits / merged.df$total_visits
  55. merged.df$yearmo <- year(merged.df$hit_date)*100 + month(merged.df$hit_date)
  56.  
  57. final_dataset = subset(merged.df, total_visits > 0)
  58.  
  59.  
  60. # Visualization - boxplot by month
  61. # Main plot, minus y axis tick labels
  62. boxplot(pct_np~yearmo,data=final_dataset, main="Google (not provided)\\nPercentage of Total Organic Searches",
  63.         xlab="Year-Month", ylab="Percent (not provided)", col= "orange", ylim=c(0,.8), yaxt="n")
  64.  
  65. #Create tick sequence and format axis labels
  66. ticks <- seq(0, .8, .2)
  67. label_ticks <- sprintf("%1.f%%", 100*ticks)
  68. axis(2, at=ticks, labels=label_ticks)
');