-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebsite-analytics.R
67 lines (41 loc) · 2.03 KB
/
website-analytics.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#### Created 3/16 by Daniel Hadley to download and upload Somerville's website data ####
# working Directory: the part you change for your machine #
setwd("C:/Users/mmastrobuoni.CH2SOM-MMASTROB/Documents/GitHub/Somerville_Data_Pipes")
# This pulls in the credentials you need
# Nothing from this, but make sure to copy the oauth_token when you copy the repository
library(RGoogleAnalytics)
library(RCurl)
library(dplyr)
library(tidyr)
# Load the token object
load("oauth_token")
ValidateToken(oauth_token)
# Create a list of Query Parameters
query.list <- Init(start.date = as.character(Sys.Date()-1),
end.date = as.character(Sys.Date()),
dimensions = "ga:pageTitle",
metrics = "ga:sessions,ga:pageviews",
max.results = 1000,
table.id = "ga:26776898")
# Create the query object
ga.query <- QueryBuilder(query.list)
# Fire the query to the Google Analytics API
ga.df <- GetReportData(ga.query, oauth_token)
#### Top from last day ####
# dates
today <- Sys.Date()
yesterday <- today - 1
time <- Sys.time()
ga.df$pageTitle <- gsub("| City of Somerville Website", "", ga.df$pageTitle)
write.csv(ga.df, "//cos-chb-share1/Somerstat Data/Website_Analytics/LastTwentyFour.csv")
write.csv(ga.df, "//cos-chb-share1/Somerstat Data/Website_Analytics/data_pipeline_pls_dont_use/LastTwentyFour.csv")
#### Now we add an output to the check_pipes file ####
check_the_pipes <- read.csv("./check-the-pipes.csv", stringsAsFactors = FALSE)
# First I am just going to add today's date to show when the script ran
check_the_pipes[which(check_the_pipes$data_set == "Website Analytics"), 2] <- as.character(Sys.Date())
# Now a simple message on each saying whether the data was downloaded or not
check_the_pipes[which(check_the_pipes$data_set == "Website Analytics"), 3] <-
ifelse(length(ga.df) < 2,
"Error dowloading latest data from Google API",
"Downloaded latest data from Google API")
write.csv(check_the_pipes, "./check-the-pipes.csv", row.names = FALSE)