Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Trends Category Search #89

Closed
stochastiq opened this issue Apr 5, 2016 · 18 comments
Closed

Google Trends Category Search #89

stochastiq opened this issue Apr 5, 2016 · 18 comments

Comments

@stochastiq
Copy link

Google trends has the option of choosing categories of search, rather than only terms. For example, I can choose to view "Shopping" category without entering any term to see the trend of all shopping related terms searched on google. How do I retrieve this series via the gtrendsR API? Furthermore, is there documentation for which number corresponds to which category on Google Trends?

@PMassicotte
Copy link
Owner

It is not supported at the moment, but it could be implemented simply I think.
https://www.google.com/trends/explore#cat=0-47-1190&date=1%2F2012%2012m&cmpt=q&tz=Etc%2FGMT-2

@stochastiq
Copy link
Author

Hi Phillip,

Thank you for your reply.

When I choose the "Shopping" category (without including search terms) this is the url that pops up:
https://www.google.com/trends/explore#cat=0-18&q=&geo=SG&cmpt=q&tz=Etc%2FGMT-2

Correct me if I'm wrong, corresponding to this URL, the gtrendsR command looks something like this:
shop_trend<-gtrends(query="",geo=c("SG"), cat="0-18", start_date= "2004-01-01", end_date = "2016-04-05")

However, the command does not allow non-characters as a value for the "query" argument. So, I tried changing the url by adding a "&" to the query
https://www.google.com/trends/explore#cat=0-18&q=&&geo=SG&cmpt=q&tz=Etc%2FGMT-2

This url gives me the "Shopping" category series without any search terms. So the corresponding command I used is:
shop_trend<-gtrends(query="&",geo=c("SG"), cat="0-18", start_date= "2004-01-01", end_date = "2016-04-05")

However, the trends from shop_trend are different from that if I were to download the csv file from google trends directly. Firstly, the csv file contains figures in %, while the shop_trends trends are an index. Taking the percentage difference of the shop_trends trends also does not return the same figures as the csv file. Secondly, the csv file's top searches and shop_trends top searches differ, though they are in the same category.

Wonder if you would know how to implement the categories search for google trends using the existing gtrends command?

@PMassicotte
Copy link
Owner

If you are writing

shop_trend <- gtrends(query="&",geo=c("SG"), cat="0-18",
 start_date= "2004-01-01", end_date = "2016-04-05")

this means that you are searching the "&" character in the shopping category. I will try to implement this this week.

@stochastiq
Copy link
Author

Ok, just as I suspected. I thought there would be a chance that it searches an empty string since the url
https://www.google.com/trends/explore#cat=0-18&q=&&geo=SG&cmpt=q&tz=Etc%2FGMT-2
shows the category search.

Anyway, thank you for your response, look forward to this extension :)

@PMassicotte
Copy link
Owner

Just started to implement this.

You can try it by installing the package with:

devtools::install_github("PMassicotte/gtrendsR", ref = "queries-on-categories")

> ch <- gconnect(usr, psw, verbose = TRUE)
Google login successful!
> data <- gtrends(geo=c("SG"), cat="0-18")
> head(data$trend)
       start        end shopping.SG
1 2004-01-04 2004-01-10           0
2 2004-01-11 2004-01-17           4
3 2004-01-18 2004-01-24           3
4 2004-01-25 2004-01-31          -1
5 2004-02-01 2004-02-07          -1
6 2004-02-08 2004-02-14           0
> plot(data)
> data <- gtrends(geo=c("CA"), cat="0-20-263", res = "1d")
> head(data$trend)
                start sporting.goods.CA
1 2016-04-06 14:00:00                 0
2 2016-04-06 14:08:00               -12
3 2016-04-06 14:16:00                 6
4 2016-04-06 14:24:00                 1
5 2016-04-06 14:32:00                -6
6 2016-04-06 14:40:00                -3
> plot(data)

rplot

rplot01

@stochastiq
Copy link
Author

Hi Phillip,

Thanks for getting back to me. It appears that I must not be doing
something right, because I still get the error:
Error in names(trend) <- make.names(paste(kw, geo)) :
'names' attribute [1] must be the same length as the vector [0]

I reinstalled the packages, and used the code:

library(gtrendsR)
user <- "XXXXX"
psw <- "XXXXX"
gconnect(user, psw, verbose = TRUE)
shop_trend<-gtrends(query="",geo=c("SG"), cat="0-18", start_date=
"2004-01-01", end_date = "2016-04-05")

Referring to your latest reply, may I ask what do you mean by:
"You can try it by installing the package with:

`i"
When I run your code above, I get this error:
Error in stopifnot(is.character(query), is.vector(query), length(query) <=
:
argument "query" is missing, with no default

Thank you for your help.

On Thu, Apr 7, 2016 at 9:59 PM, Philippe Massicotte <
notifications@github.com> wrote:

Just started to implement this.

You can try it by installing the package with:

`i

ch <- gconnect(usr, psw, verbose = TRUE)Google login successful!> data <- gtrends(geo=c("SG"), cat="0-18")> head(data$trend)
start end shopping.SG1 2004-01-04 2004-01-10 02 2004-01-11 2004-01-17 43 2004-01-18 2004-01-24 34 2004-01-25 2004-01-31 -15 2004-02-01 2004-02-07 -16 2004-02-08 2004-02-14 0> plot(data)> data <- gtrends(query = "", geo=c("CA"), cat="0-20-263", res = "1d")> head(data$trend)
start sporting.goods.CA1 2016-04-06 14:00:00 02 2016-04-06 14:08:00 -123 2016-04-06 14:16:00 64 2016-04-06 14:24:00 15 2016-04-06 14:32:00 -66 2016-04-06 14:40:00 -3> plot(data)

[image: rplot]
https://cloud.githubusercontent.com/assets/4519221/14353494/99b06966-fcd9-11e5-8a40-db276f673089.png

[image: rplot01]
https://cloud.githubusercontent.com/assets/4519221/14353499/9d0021b0-fcd9-11e5-871f-7471ee5051a6.png


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#89 (comment)

@PMassicotte
Copy link
Owner

You have to install this specific branch if you want to try it. You have to type it in R.

devtools::install_github("PMassicotte/gtrendsR", ref = "queries-on-categories")

@dap2rb
Copy link

dap2rb commented May 26, 2016

Hi PMassicotte,

thank you for your package. Could you clarify two issues:

  1. How to install the branch. I type: "devtools::..." in R and it gives me the following response: "Error in loadNamespace(name) : there is no package called ‘devtools’"
  2. How do I download items for the cities. I tried maimi with geo=US-FL-528 and it did not work

thank you a lot

@eddelbuettel
Copy link
Collaborator

For Pete's sake please don't hijack random other threads. Please delete the message above, @dap2rb .

@BERENZ
Copy link

BERENZ commented Jun 23, 2016

Dear All,

my students (@KasiaaZ and @MrKrzysztof) prepared list of categories that are used in Google Trends (included in MS Excel file google_trends_categories.xlsx).

Maybe you would like to include this list into package vignette?

Best, Maciej

google_trends_categories.xlsx

@PMassicotte
Copy link
Owner

Hi @BERENZ

That sounds like a good idea. How did you retrieved it? I downloaded the json category file from Google a while back, but ran out of time to process it.

@BERENZ
Copy link

BERENZ commented Jun 23, 2016

Hi @PMassicotte

It wasn't automated...I think they just used a copy-paste method.:)

Do you have a link to download json file? I could write a script to parse the categories.

@PMassicotte
Copy link
Owner

@BERENZ

categories.json.zip

You can read them using:

categories <- jsonlite::fromJSON(txt = "data/categories.json", flatten = TRUE)

@BERENZ
Copy link

BERENZ commented Jun 23, 2016

@PMassicotte thanks! I will provide do it tommorrow.

Where did you find this file? Is it somewhere online?

@PMassicotte
Copy link
Owner

PMassicotte commented Jun 23, 2016

@BERENZ I extracted it from my Chrome browser using the dev console while browsing Google Trends web site. CTRL + SHIFT + C

@jayruffell
Copy link

Hi, is it still possible to get search volumes for a given category without providing a keyword? The branch that this feature was on devtools::install_github("PMassicotte/gtrendsR", ref = "queries-on-categories") doesn't seem to exist anymore. Thanks

@PMassicotte
Copy link
Owner

I think you can do it with the current version.

@jayruffell
Copy link

Sorry yes should've checked the current version - code works thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants