PMassicotte / gtrendsR

R functions to perform and display Google Trends queries
354 stars 112 forks source link

Search for Company instead of Search Term #270

Open onthebanks14 opened 6 years ago

onthebanks14 commented 6 years ago

Google Trends gives you the option to see results for just the company not the search term. For example, I want to see results for "Apple" the company not "Apple" the search term. Is this functionality possible in gtrendsR?

onthebanks14 commented 6 years ago

Hacker way is just to enter the q= part of the url into as the gtrendsR keyword, but this might be a nice functionality to build in if it hasn't already. Thanks!

PMassicotte commented 6 years ago

Do you have an example?

Ayleswoj commented 6 years ago

I am looking to do this same thing. I have tried the way that onthebanks14 suggests using the native ULRdecode function on q= and cant get it to work.

Here is the raw URL from googletrends: https://trends.google.com/trends/explore?geo=US&q=%2Fm%2F01n073,battlefront%202

and the code I have been experimenting with:

a1 = gtrends(c("/m/01n073", "Battlefront 2"), geo="US", gprop = "web")$interest_over_time

gtrendsr

Ayleswoj commented 6 years ago

I want to bump this thread to see if I can get further assistance.

PMassicotte commented 6 years ago

Is this code working?

a1 = gtrends(c("/m/01n073", "Battlefront 2"), geo="US", gprop = "web")$interest_over_time
Ayleswoj commented 6 years ago

It won't work on my machine the following error is generated: Error: widget$status_code == 200 is not TRUE

mamut86 commented 6 years ago

The first query string is a Google Knowledge Graph entity. See #92 for a similar discussion on how to obtain.

PMassicotte commented 6 years ago

Can it be merged with gtrends?

mamut86 commented 6 years ago

Sorry, previous info with removing "/" was wrong.

It seems when the topic query run is on its own, it will return results but does not allow for combinations? a1 = gtrends("/m/01n073", geo="US", gprop = "web")$interest_over_time

It also works when combining two topic queries, e.g. Electronic Arts (Company) and Battlefront 2 (Video Game)

a1 = gtrends(c("/m/01n073", "/m/069jjt"), geo="US", gprop = "web")

I can't spot why it does not like to have combinations of a topic and search term.

janush1985 commented 6 years ago

Awesome lead!

Is there a scaled way of decoding terms into these "fancy" codes etc. "/m/01n073"?

mamut86 commented 6 years ago

@janush1985 see issue #92

mamut86 commented 6 years ago

@PMassicotte have a look into the pull request. I think this should fix the problem @Ayleswoj experienced.

SK0rnev commented 5 years ago

Any solution? I want to combine searches for 60+ items, half of them are "/m/xxxxx".
a1 = gtrends(c("/m/01n073", "Battlefront 2"), geo="US", gprop = "web")$interest_over_time doesn't work...

willglanville commented 5 years ago

+1 This seems to be an issue again

JBleher commented 5 years ago

It seems that currently this is a problem with the request made by get_widget. I have compared the HTTP-requests on Google Trends and the one made by the current dev-version. I have also altered the current dev-version so that the HTTP-request are exactly the same, down to the letter. No avail. Something else must be going on here... I am not sure what.

So to be more specific, the request made by a Browser is: https://trends.google.com/trends/api/explore?hl=en-US&tz=-120&req=%7B%22comparisonItem%22:%5B%7B%22keyword%22:%22FIFA%22,%22geo%22:%22US%22,%22time%22:%22today+5-y%22%7D,%7B%22keyword%22:%22%2Fm%2F01n073%22,%22geo%22:%22US%22,%22time%22:%22today+5-y%22%7D%5D,%22category%22:0,%22property%22:%22%22%7D&tz=-120

The decoded version is: https://trends.google.com/trends/api/explore?hl=en-US&tz=-120&req={\"comparisonItem\":[{\"keyword\":\"FIFA\",\"geo\":\"US\",\"time\":\"today+5-y\"},{\"keyword\":\"/m/01n073\",\"geo\":\"US\",\"time\":\"today+5-y\"}],\"category\":0,\"property\":\"\"}&tz=-120

I tweaked the url-building lines in the code in get_widget() as follows: url <- paste0(URLencode(paste0("https://trends.google.com/trends/api/explore?hl=", hl, "&tz=", tz, "&req=")), URLencode(paste0(jsonlite::toJSON(token_payload, auto_unbox = TRUE)), reserved = TRUE), URLencode(paste0("&tz=",tz)))

The problem is still there. So even when one submits the same URL request, with the same parameters, I get a HTTP-400 answer.

PMassicotte commented 5 years ago

Any idea on how to fix it?

JBleher commented 5 years ago

No. Unfortunately not. Maybe it's a cookie thing? I am puzzled...