-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
170 lines (132 loc) · 4.47 KB
/
app.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
library(shiny)
library(DT)
library(plotly)
# Define UI ----
ui <- fluidPage(
#upload and settings
titlePanel("Compare search queries CTR VS Average Position"),
mainPanel(
fileInput(
"file1",
"1. 'Queries.csv' File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
textInput("brand", "2. brand (regex)", "brand name|brandname"),
textInput("filter", "3. filter (regex)", ".*"),
checkboxInput("mean", "Mean CTR by Avg. Pos.", FALSE),
checkboxInput("median", "Median CTR by Avg. Pos.", FALSE)
#sliderInput(inputId = "impression", label = "Impression", min = 0, max = max(df$Impressions), value = c(0, max(df$Impressions)), step = 5)
),
#howto section
h2("How to use this tool?"),
p(
"Upload your Queries.csv file from Google Search Console, fill in you brand and to compare each search queries CTR VS avg. Position."
),
p(
"Checkout search queries on the top left, they have good positions and bad CTR."
),
p(
"You might want rework a few metadata to increase CTR and secure more SEO traffic. The darker a dot is, the more impressions it represents.
Ideally we should to remove data from non-targeted countries and remove Google Image search data."
),
hr(),
tabsetPanel(
tabPanel("ALL", fluid = TRUE,
mainPanel(plotlyOutput("contents2"))),
tabPanel("Explorer", fluid = TRUE,
mainPanel(DTOutput("contents")))
)
)
# Define server logic to read selected file ----
server <- function(input, output) {
#graph
output$contents2 <- renderPlotly({
#need files to upload
req(input$file1)
tryCatch({
df <- read.csv(input$file1$datapath,
header = TRUE)
#sep = ",",
#quote = input$quote)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
})
#rename colonne because sometimes colname doesn't match in other language
colnames(df) <-
c("Query", "Clicks", "Impressions", "CTR", "Position")
df <- droplevels(df)
# transform CTR to a number
df$CTR <-
as.numeric(gsub(pattern = "%", replacement = "", df$CTR))
df <- na.omit(df)
# add branding tag according to setting
df <- df %>%
#mutate(branding = stringr::str_detect(Query, "green"))
mutate(branding = stringr::str_detect(Query, input$brand)) %>%
filter(grepl(input$filter, Query))
# average and mean computation per positions
df_mean <- df
df_mean$Position <- round(df$Position)
# grabing max impression to be sure dot are being display with 1 opacity
max <- max(df_mean$Impressions)
df_median <- df_mean
df_mean <- df_mean %>%
group_by(Position) %>%
summarise(CTR = mean(CTR), Impressions = max)
df_median <- df_median %>%
group_by(Position) %>%
summarise(CTR = median(CTR), Impressions = max)
df_mean$Query <- paste("Mean Pos.", df_mean$Position)
df_median$Query <- paste("Median Pos.", df_mean$Position)
#install.packages("scales")
library(scales)
p <- ggplot(data = df) +
aes(
x = CTR ,
y = Position,
label = Query,
alpha = Impressions,
color = branding
) +
geom_point() +
theme_minimal() +
scale_x_continuous(label = label_number(suffix = "%")) +
scale_y_reverse(lim = c(15, 1)) +
scale_alpha(range = c(0.3, 1)) +
scale_color_manual(values = c("#09043a", "#ff0000")) +
labs(title = "POS 1", x = "SERPs CTR", y = "Avg. Google Position") +
theme(legend.position = "bottom") +
scale_alpha(range = c(0.05, 1), trans = "log")
if (input$mean) {
p <- p + geom_point(data = df_mean,
color = 'gold',
size = 3)
}
if (input$median) {
p <- p + geom_point(data = df_median,
color = 'green',
size = 3)
}
#render plotly using ggplot graph
print(ggplotly(p))
})
output$contents <- renderDT({
req(input$file1)
tryCatch({
df <- read.csv(input$file1$datapath,
header = T,
sep = ",")
},
error = function(e) {
stop(safeError(e))
})
return(df)
})
}
# Create Shiny app ----
shinyApp(ui, server)