-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrga.r
110 lines (77 loc) · 3.04 KB
/
rga.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#R Google Analytics with a for loop to run through all the properties!!
#install.packages("RCurl")
#install.packages("rjson")
require("RCurl")
require("rjson")
library("RGoogleAnalytics", lib.loc="C:/Users/jglenn/Documents/R/win-library/3.0")
# Loading the RGoogleAnalytics library
require("RGoogleAnalytics")
# Step 1. Authorize your account and paste the accesstoken
query <- QueryBuilder()
access_token <- query$authorize()
# Step 2. Initialize the configuration object
conf <- Configuration()
# Retrieving a list of Accounts
ga.account <- conf$GetAccounts()
ga.account
# Retrieving a list of Web Properties
# With passing parameter as (ga.account$id[index]) will retrieve list of web properties under that account
ga.webProperty <- conf$GetWebProperty()
ga.webProperty
# Retrieving a list of web profiles available for specific Google Analytics account and Web property
# by passing with two parameters - (ga.account$id,ga.webProperty$id).
# With passing No parameters will retrieve all of web profiles
ga.webProfile <- conf$GetWebProfile()
ga.webProfile
# Retrieving a list of Goals
# With passing three parameters (ga.account$id[index],ga.webProperty$id[index], ga.webProfile$id[index])
# will retrieve specific goals
#ga.goals <- conf$GetGoals()
#ga.goals
# For retrieving a list of Advanced segments
#ga.segments <- conf$GetSegments()
#ga.segments
# Step 3. Create a new Google Analytics API object
ga <- RGoogleAnalytics()
# Old way to retrieve profiles from Google Analytics
ga.profiles <- ga$GetProfileData(access_token)
# List the GA profiles
ga.profiles
#I need to try this next, i think this will work :)
# Step 4. Setting up the input parameters
filter.mobileSite <- "ga:hostName=~^m\\."
filter.desktopSite <- "ga:hostname!~siteencore\\.com|^m\\."
ga.data <- NULL
uri <- NULL
df <- NULL
for (i in 1:nrow(ga.profiles)) {
tryCatch({
profile <- ga.profiles$id[i]
startdate <- "2013-04-01"
enddate <- "2013-04-30"
dimension <- NULL
metric <- "ga:visitors, ga:pageViews, ga:visits, ga:timeOnSite, ga:avgTimeOnSite"
filter <- filter.desktopSite
segment <- NULL
sort <- "-ga:visits"
maxresults <- 99
# Step 5. Build the query string, use the profile by setting its index value
query$Init(start.date = startdate,
end.date = enddate,
dimensions = dimension,
metrics = metric,
sort = sort,
filters= filter,
segment=segment,
max.results = maxresults,
table.id = paste("ga:",profile,sep="",collapse=","),
access_token=access_token)
# Step 6. Make a request to get the data from the API
ga.data <- ga$GetReportData(query)
ga.data <- cbind(ga.webProperty[i,], ga.data)
df <- rbind(df, ga.data )
#If you want to see the final generated URL, use:
#uri[i] <- query$to.uri()
}, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
write.csv(df, file = "RGA df.csv")