Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Google Analytics + R. Praktyczne przykłady.

896 Aufrufe

Veröffentlicht am

Spotkanie Entuzjastów R, Politechnika Warszawska, 17.03.2016.
Przykłady: wizualizacja danych, klasteryzacja użytkowników, wykrywanie anomalii, prognozowanie w Google Analytics i R.

Veröffentlicht in: Technologie

Google Analytics + R. Praktyczne przykłady.

  1. 1. 100 150 00:01:20 120 300 00:02:15
  2. 2. ● ● ● ● ● ●
  3. 3. require(RGoogleAnalytics) client.id <- "xxxxxxxxxxxx.apps.googleusercontent.com" client.secret <- "zzzzzzzzzzzz" token <- Auth(client.id,client.secret) # Save the token object for future sessions save(token,file="./token_file")
  4. 4. # Get the Sessions by Month in 2014 query.list <- Init(start.date = "2014-01-01", end.date = "2014-01-31", dimensions = "ga:date", metrics = "ga:sessions", table.id = "ga:000000") # Create the Query Builder object ga.query <- QueryBuilder(query.list) # Extract the data and store it in a data-frame ga.data <- GetReportData(ga.query, token)
  5. 5. > head(ga.data) date sessions 1 20140101 39 2 20140102 46 3 20140103 47 4 20140104 53 5 20140105 49 6 20140106 15
  6. 6. # Get the Sessions by dayOfWeek in 2013 query.list <- Init(start.date = "2013-01-01", end.date = "2013-12-31", dimensions = "ga:dayOfWeek,ga:date", metrics = "ga:sessions", table.id = "ga:000000") # Processing Query to ga.data ... ggplot(data = ga.data, aes(x = dayOfWeek, y = sessions)) + geom_boxplot()
  7. 7. # Get the Sessions by date in 2013-2015 query.list <- Init(start.date = "2013-01-01", end.date = "2015-12-31", dimensions = "ga:date", metrics = "ga:sessions", table.id = "ga:000000") # Processing Query to ga.data ...
  8. 8. # Plot for calendar calendar_sessions <- ggplot(data, aes(monthweek, weekdayf, fill = sessions)) + geom_tile(colour = "white") + facet_grid(year~monthf) + scale_fill_gradient(high="#D61818",low="#B5E384") + labs(title = "Kalendarz") + xlab("Tydzien miesiaca") + ylab("") # View calendar calendar_sessions
  9. 9. install.packages("devtools") devtools::install_github("twitter/AnomalyDetection") library(AnomalyDetection) # Processing Query to ga.data ... # Anomaly detection res = AnomalyDetectionTs(ga.data, max_anoms=0.02, direction='both', plot=TRUE) res$plot
  10. 10. ... library(forecast) ... forecast <- forecast.HoltWinters(forecastmodel, h=26) # 26 days in future ...
  11. 11. ● ●
  12. 12. ● ○ ● ○
  13. 13. <script> dataLayer = [{ 'level': 'advanced', 'fingerprint' : '123456' }]; </script> https://goo.gl/1X84fY
  14. 14. # Get the Sessions by Month in 2014 query.list <- Init(start.date = "2014-01-01", end.date = "2014-12-31", dimensions = "ga:dimension01, ga:contentGroup01", metrics = "ga:contentGroupUniqueViews01", table.id = "ga:000000") # Create the Query Builder object ga.query <- QueryBuilder(query.list) # Extract the data and store it in a data-frame ga.data <- GetReportData(ga.query, token)
  15. 15. > head(ga.data) Beginner Intermediate Advanced 191352 0 2 42 990977 0 4 32 770561 0 4 48 898022 0 5 21 277510 0 6 31 644227 0 6 44
  16. 16. # K-Means Cluster Analysis fit <- kmeans(ga.data, 3) # 3 clusters ... # Append cluster assignment data.fit <- data.frame(ga.data, fit$cluster) head(data.fit)
  17. 17. > data.fit Beginner Intermediate Advanced fit.cluster 266876 9 45 4 1 965265 9 51 7 1 ... 981924 19 10 8 2 732529 19 16 1 2 ... 377795 2 7 38 3 918083 2 8 28 3
  18. 18. > fit$centers Beginner Intermediate Advanced 1 7.011765 38.42353 5.023529 # level: intermediate 2 25.530435 10.06087 4.713043 # level: beginner 3 3.628571 5.90000 32.657143 # level: advanced
  19. 19. # 3d chart install.packages("plotly") library(plotly) result <- data.frame(df, fit$cluster) plot_ly(result, x = result$level1, y = result$level2, z = result$level3, type = "scatter3d", mode = "markers", color=factor(result$fit.cluster))

×