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:

library(httr)

getreq <- GET("https://trends.google.com/trends/explore",
    query=list(q = "Covid",geo = "US"))

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:

getreq[["url"]]
[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 

gtrendsdat <- gtrends("Covid",geo=c("US"))

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 

gtrendsdat <- gtrends("Covid",geo=c("US", "GB", "FR"))

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:

gtrendsdat  <- load(gzcon(url("https://github.com/cjbarrie/CS-ED/blob/main/data/floyd.RData?raw=true")))

These data are from a paper of my own that you can access here.

Questions

  1. Plot the results by country over time

  2. Place a line at the time of George Floyd’s murder to show how trends changed before and after.