PMassicotte / gtrendsR

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

Restricting keyword searches to collate info just on cities #443

Closed alan-duncan closed 1 year ago

alan-duncan commented 1 year ago

Hi,

This is a great package, thank you so much.

I have a (hopefully) quick and naive query. I'm looking to collect search data for Perth (the city in Australia) from different countries, replicating search data produced by the Explore function within Google Trends.

Capture

However, I can't find the GTrendsR syntax that restricts the search to refer only to Perth as the Australian city (the option in the online Google Trends platform) rather than a general search for the word Perth:

gtrends(keyword = c("Perth"), geo = c("GB"), category = 67, gprop = "web", time = "today 1-m")$interest_over_time

The problem is that this includes search info for Perth in Scotland, Perth Huron etc. Any advice on how I can restrict gtrends to look only for searches for a specific city would be much appreciated.

Many thanks in anticipation.

Alan

eddelbuettel commented 1 year ago

Does the Google Trends web interface let you constrain searches to particular meanings for a term?

PMassicotte commented 1 year ago

This is likely related to #326.

Maybe something like this (not tested)?

gtrends(keyword = c("/m/062qg"), geo = c("GB"), category = 67, gprop = "web", time = "today 1-m")
alan-duncan commented 1 year ago

Hi both,

Thanks so much for getting back to me so quickly - very much appreciated. And Philippe, your solution works perfectly and entirely replicates the data that comes via the Google Trends online dashboard.

One final quick question - could you please point me to the resource that gave you the "/m/062qg" code for Perth as a city in Australia? I've tried to find a source from your #326 suggestion but to no avail - sorry for being so slow on the uptake.

Thank you again.

Best,

Alan

eddelbuettel commented 1 year ago

That's actually a solution from @JBleher that we just forwarded :)

PMassicotte commented 1 year ago

Just used the code provided in the URL:

image

r$> URLdecode("%2Fm%2F062qg")
[1] "/m/062qg"
PMassicotte commented 1 year ago

Would be nice to have a list of all possible codes, but we could not find any place or data source to parse.

JBleher commented 1 year ago

Hey there, yes that's how I proceeded as well. Look for a topic in Google-Trends and then URLdecode the identifier. Usually, if it is something global the identifier starts with "/m/#####", if it is a national identifier, for Germany I think it starts with "/g/####". If it is a global topic then usually there are 6 alphanumeric characters, if it is a national topic there can be more. I suspect the numbers are some sort of hashing... so maybe if you have enough identifiers, you can decrypt the hash-function ;D but I guess you need a lot ... of hashes and time...

alan-duncan commented 1 year ago

Thank you all for such great suggestions and expertise in solving my query. In the words of Ted Lasso, I really appreciate you all :) Best, Alan