library(httr)
<- GET("https://trends.google.com/trends/explore",
getreq query=list(q = "Covid",geo = "US"))
Worksheet 2: Using APIs
In this worksheet, we will be going through an example of using the Google Trends API.
Google Trends can—and has—been used in research to give us information on the broader context of a given phenomenon, or might even be used as the main core of empirical data (e.g., Brodeur et al. (2021)).
In what follow, we will get to grips with what an API is, and use a particular R package called gtrendsR
to query the Google Trends API.
The examples for this worksheet were adapted from Bauer and Landesvatter (2022).
We can start by looking at the bare bones of an API request. To do this, we will use the httr2
package:
This returns a list containing some data as well as the various parameters that we sent to the Google Trends API. We can have a look at these as follows:
"url"]] getreq[[
[1] "https://trends.google.com/trends/explore?q=Covid&geo=US"
This shows the url that we sent to the API. And that is all an API request really is. Here, we’re just specifying the place (US) and the keyword (Covid). Everything else is set to its default.
A more straightforward way of specifying different parameters in an API call is by using a pre-packaged library optimized for querying it.
Let’s see what the same call would look like using gtrendsR
:
library(gtrendsR)
data("countries") # get abbreviations of all countries to filter data
data("categories") # get numbers of all categories to filter data
<- gtrends("Covid",geo=c("US")) gtrendsdat
And then we could introduce different countries:
library(gtrendsR)
data("countries") # get abbreviations of all countries to filter data
data("categories") # get numbers of all categories to filter data
<- gtrends("Covid",geo=c("US", "GB", "FR")) gtrendsdat
Alternative exercise
Since the Google API seems to have changed, gtrendsR
doesn’t seem to be working as of today (17/10/23). As such, I have instead provided you with some data you can download.
You can do this locally on your computers with:
<- load(gzcon(url("https://github.com/cjbarrie/CS-ED/blob/main/data/floyd.RData?raw=true"))) gtrendsdat
These data are from a paper of my own that you can access here.
Questions
Plot the results by country over time
Place a line at the time of George Floyd’s murder to show how trends changed before and after.