-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdobeAnalyticsCountryVisitsMap.R
172 lines (129 loc) · 6.95 KB
/
AdobeAnalyticsCountryVisitsMap.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#---------Connecting to Adobe Analytics and creating website geosegmentation Map----------------------
#Author: Alice Daish (British Museum adaish@britishmuseum.org)
#September 2016
#https://github.com/BritishMuseum/RWebGeoSegMap
#------------PACKAGE IN USE IS RSITECATALYST-----------------------------------------------------------
#https://github.com/randyzwitch/RSiteCatalyst
#------------INSTALL PACKAGES--------------------------------------------------------------------------
#install.packages("devtools")
library(devtools)
#install_github("randyzwitch/RSiteCatalyst", ref="master")
library(RSiteCatalyst)
#------Install dependant packages-----------------------------------------------------------------------
#install.packages("jsonlite")
#install.packages("plyr")
#install.packages("httr")
#install.packages("stringr")
#install.packages("digest")
#install.packages("base64enc")
#install.packages("RCurl")
library("jsonlite")
library("plyr")
library("httr")
library("stringr")
library("digest")
library("base64enc")
library("RCurl")
#-------Authentication----------------------------------------------------------------------------------
#Find web service credentials in User Management in the marketing cloud
#Log in for adobe analytics
SCAuth("username:Company", "secretkey")
#----------Explore Report Suite and Metrics, Evars and Props--------------------------------------------
#Report Suites
report_suites <- GetReportSuites()
report_suites
#Find eVars
eVars<-GetEvars("ReportSuiteName")
eVars
#Find metrics
metrics<-GetMetrics("ReportSuiteName")
metrics$id #list of metric id available
metrics[which(metrics$id =="visits"),] #visits
metrics[which(metrics$id =="uniquevisitors"),] #uniquevisitors
#Find elements
elements <- GetElements("ReportSuiteName")
elements$id #list of elements id available
elements[which(elements$id =="geocountry"),] #country
#GetSuccessEvents - pass a single argument or concatentated vector
events <- GetSuccessEvents("ReportSuiteName")
events <- GetSuccessEvents(report_suites$rsid)
events
#GetProps - pass a single argument or concatentated vector
trafficvars <- GetProps("ReportSuiteName")
trafficvars
#-------------- Finding Visits and Unique Website Visitors by Country----------------------------------------------------------------
countryvisits<-QueueRanked("ReportSuiteName", #Select Report Suite
date.from="2015-09-01", #Start date for example "2015-09-01" to
date.to="2015-09-01", #End date for example "2015-09-01"
metrics=c("visits","uniquevisitors"), #Visits and Unique Visitors will be returned
elements="geocountry",
top=300) #Top 300 (all countries)
#View data
View(countryvisits)
#Save data - best to save as some of the countries Adobe Analytics and rworldmap packages are differently named
write.csv(countryvisits,"20150901_20150831_countriesdata.csv")
#---------------Create a World map of Website GeoSegmentation Visit ----------------------------------------------------------------
#install.packages("rworldmap")
library(rworldmap)
#Data Prep: Edit the data file as some countries names don't match from adobe analytics output to the rworldmap
#Need to remove "Slovakia Republic" from Slovakia
#Need to remove "Province of China" from Taiwan
#Need to remove "n Arab Jamahiriya" from Libya
#Need to remove "Peoples Democratic Republic" from Lao
#Load data
data<-read.csv("20150901_20150831_countriesdata.csv")
#View data
View(data)
#--------------Yellow to Red Scale Map for Visits or Visitors ----------------------------------------------------------------------------
#--------------Plot Visits by Country Map
#Plot numbers on yellow to red colour spectrum map (with automated scale legend)
#Create a map-shaped window
mapDevice('x11')
#Joining the data to a map
map <- joinCountryData2Map(data, joinCode="NAME", nameJoinColumn="name")
#Display the visits map
map1<-mapCountryData(map, nameColumnToPlot="visits",mapTitle="Visits by country",catMethod="fixedWidth",addLegend=T)
#-------------Plot Unique Visitors by Country Map
#Plot numbers on yellow to red colour spectrum map (with automated scale legend)
#Create a map-shaped window
mapDevice('x11')
#Joining the data to a map
map <- joinCountryData2Map(data, joinCode="NAME", nameJoinColumn="name")
#Display the visits map
map1<-mapCountryData(map, nameColumnToPlot="uniquevisitors",mapTitle="Unique visitors by country",catMethod="fixedWidth",addLegend=T)
#--------------Light to Dark Green Scale Map for Visits or Visitors ----------------------------------------------------------------------------
#--------------Plot Visits by Country Map--------------------
#Create a map-shaped window
mapDevice('x11')
#Joining the data to a map
map <- joinCountryData2Map( data,joinCode = "NAME",nameJoinColumn = "name" )
#Creating a user defined colour palette
op <- palette(c("#ffffcc","#c2e699","#78c679","#238443")) #Light green to dark green colour palette
#Find quartile breaks
cut<- quantile(map@data[["visits"]],na.rm=TRUE)
quantile(map@data[["visits"]],na.rm=TRUE) #Give quantile breaks for labelling
#Classify the data to a factor
map@data[["number"]] <- cut( map@data[["visits"]],cut, include.lowest=TRUE )
#rename the categories quantiles
levels(map@data[["number"]]) <- c("very low (1-99)","low (100-199)","med (200-299)","high (300-399)") #Type the legend colour quantile breaks
#Create Map
mapCountryData( map, nameColumnToPlot="number",catMethod="categorical",mapTitle="Visits by country",colourPalette="palette", oceanCol="lightblue",missingCountryCol="white")
mtext("Data Source :Adobe Analytics : Website Stats",side=1,line=-1) #Add data source text
#---------Plot Unique Visitors by Country Map-----------------
#Create a map-shaped window
mapDevice('x11')
#Joining the data to a map
map <- joinCountryData2Map( data,joinCode = "NAME",nameJoinColumn = "name" )
#Creating a user defined colour palette
op <- palette(c("#ffffcc","#c2e699","#78c679","#238443")) #Light green to dark green colour palette
#Find quartile breaks
cut<- quantile(map@data[["uniquevisitors"]],na.rm=TRUE)
quantile(map@data[["visits"]],na.rm=TRUE) #Give quantile breaks for labelling
#Classify the data to a factor
map@data[["number"]] <- cut( map@data[["uniquevisitors"]],cut, include.lowest=TRUE )
#Rename the categories quantiles
levels(map@data[["number"]]) <- c("very low (1-99)","low (100-199)","med (200-299)","high (300-399)")
#Create Map
mapCountryData( map, nameColumnToPlot="number",catMethod="categorical",mapTitle="Unique Visitors by country",colourPalette="palette", oceanCol="lightblue",missingCountryCol="white")
mtext("Data Source : Adobe Analytics : Website Stats ",side=1,line=-1) #Add data source text
#--------------------------------------------END-------------------------------------------------------------------------------------------------