-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_trends.R
More file actions
executable file
·97 lines (72 loc) · 5.03 KB
/
google_trends.R
File metadata and controls
executable file
·97 lines (72 loc) · 5.03 KB
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
if(!require(tidyverse)) install.packages("tidyverse",
repos = "http://cran.us.r-project.org")
if(!require(gtrendsR)) install.packages("gtrendsR",
repos = "http://cran.us.r-project.org")
## https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf
getwd()
wd <- '/Users/danielromotsky/Dropbox/Portfolio/searchscripts' # set working directory where keyword list is
setwd(wd)
getwd()
filename <- 'keywords.csv'
output_rising <- 'rising.txt'
output_trending <- 'trending.txt'
## pull in keywords from csv
keywords <- as.vector((read.csv(filelist[filelist==filename], header = TRUE, na.strings = FALSE, stringsAsFactors = FALSE)))
data("categories") # load category data
data("countries") # load country data
countries %>% filter(country_code=='US') #view list of DMAs in US
countries_rollup <- as.character(unique(factor(countries$country_code)))
# photoshop <- gtrends(keyword = "photoshop", ## A character vector with the actual Google Trends query keywords. Multiple keywords are possible using gtrends(c("NHL","NBA", "MLB", "MLS")).
# geo = "", ## A character vector denoting geographic regions for the query, default to "all" for global queries. Multiple regions are possible using gtrends("NHL", c("CA", "US")).
# time = "today 12-m", ##A string specifying the time span of the query
# gprop = c("web","news", "images", "froogle", "youtube"), ##
# category = 0, ##A character denoting the category, defaults to "0"
# hl = "en-US", ##A string specifying the ISO language code (ex.: "en-US" or "fr"). Default is "en-US". Note that this is only influencing the data returned by related topics
# low_search_volume = FALSE) ## Logical. Should include low search volume regions?
keyword_list <- c("Adobe Analytics")
kw2 <- gtrends(keyword = keyword_list, ## A character vector with the actual Google Trends query keywords. Multiple keywords are possible using gtrends(c("NHL","NBA", "MLB", "MLS")).
geo = "US", ## A character vector denoting geographic regions for the query, default to "all" for global queries. Multiple regions are possible using gtrends("NHL", c("CA", "US")).
time = "today 12-m", ##A string specifying the time span of the query
gprop = c("web","news", "images", "froogle", "youtube"), ##
category = 0, ##A character denoting the category, defaults to "0"
hl = "en-US", ##A string specifying the ISO language code (ex.: "en-US" or "fr"). Default is "en-US". Note that this is only influencing the data returned by related topics
low_search_volume = FALSE) ## Logical. Should include low search volume regions?
kw2$related_queries
## run a for loop to get rising term data for all keywords...
for (i in 1:lengths(keywords[1])) {
kw_loop2 <- gtrends(keyword = keywords$keywords[i],
geo = "US", ## A character vector denoting geographic regions for the query, default to "all" for global queries. Multiple regions are possible using gtrends("NHL", c("CA", "US")).
time = "today 12-m", ##A string specifying the time span of the query
gprop = c("web","news", "images", "froogle", "youtube"),
category = 0, ##A character denoting the category, defaults to "0"
hl = "en-US", ##A string specifying the ISO language code (ex.: "en-US" or "fr"). Default is "en-US". Note that this is only influencing the data returned by related topics
low_search_volume = FALSE) [[7]] ## Logical. Should include low search volume regions?
if(i==1){
kw_loop <- kw_loop2
}
else {kw_loop <- bind_rows(kw_loop,kw_loop2)
}
}
#select only rising terms
filter(kw_loop[c(5,3,1,2)], related_queries == 'rising')
# lapply(kw_loop, class)
# export rising terms
write.table(filter(kw_loop[c(5,3,1,2)], related_queries == 'rising'), file = output_rising, row.names = FALSE, quote = FALSE, sep = "\t")
# interest
## run a for loop to get rising term data for all keywords...
for (i in 1:lengths(keywords[1])) {
kw_loop2 <- gtrends(keyword = keywords$keywords[i],
geo = "US", ## A character vector denoting geographic regions for the query, default to "all" for global queries. Multiple regions are possible using gtrends("NHL", c("CA", "US")).
time = "today 12-m", ##A string specifying the time span of the query
gprop = c("web","news", "images", "froogle", "youtube"),
category = 0, ##A character denoting the category, defaults to "0"
hl = "en-US", ##A string specifying the ISO language code (ex.: "en-US" or "fr"). Default is "en-US". Note that this is only influencing the data returned by related topics
low_search_volume = FALSE) [[1]] ## Logical. Should include low search volume regions?
if(i==1){
kw_loop <- kw_loop2
}
else {kw_loop <- bind_rows(kw_loop,kw_loop2)
}
}
# export rising terms
write.table(kw_loop, file = output_trending, row.names = FALSE, quote = FALSE, sep = "\t")