We will be using R

First we will have a brief tutorial on GTrends is,

I’ll have a brief intro into the tutorial and explain what we’re going to do in the tutorial.

What is GTrends?

Here I’ll have a brief explainer about what GTrends is.

GTrends (also known as Google Trends) is a product of Google that analyzes the popularity of a certain search querie through different regions and languages and allows us to see how often a particular subject/keyword/topic has been search compared to the total search during that time. This product gives us insights into what the search is looking for during real time such as Millie Bobby Brown. It also can show us terms that have been looked for related to ours. It allows us to see individuals behvaior, public interests. It can allow us to motior behavioral responses during times of crisis (examples: war, political debates, pandemics, etc.) as well as how identity can shaped through social shifts and public health needs (depression during the pandemic, etc.)

Getting started

We’ll install the search package.

 # install here
install.packages("gtrendsR")
## Installing package into '/Users/benjaminsilver/Library/R/x86_64/4.2/library'
## (as 'lib' is unspecified)
## 
## The downloaded binary packages are in
##  /var/folders/pp/lk19kb4d6_g2vn0f30qf_zv40000gn/T//RtmpO2HEGF/downloaded_packages
#This package requires no API, all you have to do is install the "gtrendsR" package, get access to the package by running it through the library, you should have access to the package already as Ben has downloaded in our system for us
library(gtrendsR)

Now that we have the package installed and ready in the library, let’s try out some different functions!

Keyword

The basic function is called “keyword.” This function is rather intuitive, it simply allows you to place any inquiry into google trends and look at the results. The code chunk is as follows:

gtrends(keyword = c("nyc", geo = "US", time = 'now 7-d'))
  # We'll search for trends about our city.
  # We need to make sure to specify the time frame in the inquiry, or else R may have an issue running the chunk. In this case, we don't really care about the time frame, so we are inputting the time frame as "NULL". We will cover specifying an actual timeframe shortly. 

Keyword is the most basic chunk that we would run, but now we’re going to look at a few functions that can make our search more specific.

Country, States and Cities

# same search but with geo attached
gtrends(keyword = c("nyc", time = "NULL", geo = "US"))

# This chunk would help us gain insight into search history related to "nyc" in the US at any time. Where would I go if I want to find the specific sub code for a specific country? For example, what if I wanted to instead look up the search history for Nigeria? 

#In your console, you should see a countries section, and a grid at the very end. CLick that grid, you will see every country's sub code listed, and you can put the specific's country in the search bar in order to yield the subcode. For Nigeria, the sub code is "NG".

gtrends(keyword = c("nyc", time = "NULL", geo = "NG"))

# Awesome! Now what if we want to run multiple countries at one time? Let's try to find the results for the US, France, and Nigeria:

gtrends(keyword = c("nyc", time = "NULL", geo = "US", "FR", "NG"))

# Finally, it would be productive to learn how to search specific regions, states, or regions in gtrends. In order to do this, we would attach the city state or city's sub code to the country's sub code. Here we will print out the search "nyc" from New Jersey. It will look as follows

gtrends(keyword = c("nyc", time = "NULL", geo = "US-NJ"))

# Next, let's try to search the same inquiry, but from Los Angeles:

gtrends(keyword = c("nyc", time = "NULL", geo = "US-LA"))

# In order to find the city or state, we would similarly type it into the country search bar, which would reveal the specific sub code.

Now that we know how to search for specific countries, states, and cities, let’s learn how to specify the timeframe!

#Time In R, time is used as a parameter that can help us specific the timeframe for our search. Time has some specific features in GTrends and below we’ll include how to find time.

# Remember when we put time in as "NULL"? Now we're going to do the same thing, but instead we are going to put in a specific timeframe. In order to search timeframes by hour, we would use the sub code "h" as follows:

gtrends(keyword = c("nyc", time = "now 1-H"))

# In this chunk, we use "now" to indicate that we are searching from the present moment backwards, and we use "1-H" to indicate that the time frame is one hour. In other words, this chunk allows us to search one hour back in time. We would use this for any amount of hours. Let's try 5 hours back.

gtrends(keyword = c("nyc", time = "now 5-H"))

# For days, we would run the same chunk, but instead we would use "d" for day rather than "H" for hour. This chunk will allow us to see the last 5 days of search results.

gtrends(keyword = c("nyc", time = "now 5-d", geo = "US-CA"))

# In order to search by month, instead of saying "now", we would say "today", and use the letter "m" to find search results from the past 30 days. Each "m" accounts for 30 days (so three months won't necessarily take you from October 3rd back to July 3rd).

gtrends(keyword = c("nyc", time = "today 1-m"))

# In order to search by year, we would run the same chunk, except we would replace "m" with "y" for year, and we would add an addition sign. An inquiry to see search results from the last 2 years would look like this:

gtrends(keyword = c("nyc", time = "today+2-y"))

# In order to see all search inquiries that have ever been made related to nyc on google trends, we would use the chunk "time = all". Be aware that google trends started in 2004, so the oldest data will be 20 years old.

gtrends(keyword = c("nyc", time = "all"))

# If we want to see a specific date, we would use the format Y-m-d (year, month, day). Let's see search results from Josh's 13th birthday:

gtrends(keyword = c("nyc", time = "2016-10-29"))

# If we want to specify between two specific dates, we would do the same thing and simply add the second date in the chunk. It would be as follows:

gtrends(keyword = c("nyc", time = "2016-10-29 2016-10-30"))

#GProp GPROP allows you to specific which Google product you would like to gather the information/trends from. This is helpful because people may look at different information through different Google platforms. An example is a child might look up a YouTube video on how to tie their shoelace. Below I’ll be including the terms you’ll have to use for each of the products.

“web” (default) – Web Search This gathers data from Google’s main search engine (the one you use to look up things like what a certain word means). If you don’t use a value for gprop, it will automatically assume it is “web” as default.

“news”- Google News This option allows us to get information specifically from Google News which gets information that are news articles from different sources. (Example: Election)

“images”- Google Images This information is gathered using Google Images. Allows you to know things like how many people are search “fall clothing”

“froogle” – Google Shopping This gathers information from Google Shopping. Which gathers data from search queries made on Google Shopping like “brown hoodies”.

“youtube”- Youtube Search This gathers information from Youtube. An example of information that could be gathered is how many people are looking at “how to destress” during the pandemic.

gtrends(keyword = "nyc", gprop = "image")

Categories

In gtrendsR, there are specific numbers that connect to specific categories. There is no way to ask R to print the numbers related to categories, but you can easily search them on the internet.

# In this chunk, I will use the number 33, which represents "Art and Entertainment".
gtrends(c(keyword ="nyc", category = 33))

Host language

If you want to search for inquiries in different languages, you can use the sub code “hl” to find the different languages. Let’s look at some examples

# In this chunk, we will be searching for inquiries about the weather made in Arabic.

gtrends(c(keyword ="weather", hl = "ar"))

# We are aware that although the same language may be spoken across different countries, different countries may have different dialects, and R accounts for this by allowing us to search the country attached to the language. Let's look for inquiries made in Egyptian Arabic by using "EG" for Egypt and "ar" for Arabic (I found "EG" by looking on the country grid like I did before for location).

gtrends(c(keyword ="weather", hl = "ar-EG"))

Compared Breakdown

The compared breakdown parameter is used when comparing the search popularity of different keywords across different geographical regions. This is useful to see how different keywords perform compared to each other in different location. Very important in a psychology data collection. It is a logical parameter. When set to TRUE, it compares how popular each search keyword is across a specific region and when set to FALSE, it does not break down the comparison by city. When set to TRUE, it will show you how the search interest compares across cities or subregions.

#Compares "nyc" and "la" search trends across cities in the US in the past 12 months
gtrends(keyword = c("nyc", "la"), geo = "US", time = "today 12-m", compared_breakdown = TRUE)

Timezone(tz)

(tz) addresses the timezone which controls how the time is adjusted in the query relative to the world’s standard time. It allows us to shift the timezone based on where we want it and this is mainly used if there’s a specific time zone, you’d like to work with.

gtrends(keyword = c("nyc", "la"), geo = "US", time = "2023-05-12 2023-05-13", tz = -120)

onlyInterest

onlyInterest, is a logical parameter that determines where you want the interest over time data. You set this to true if you want the interest over time metric Google Trends provides and don’t want any other info such as how interests for “nyc” or “la” has changed and you don’t need to see the region. If you set this to FALSE, it will include further details like region, and related topics.

#Retrieving Google Trends data specifically focusing on interest over time for "nyc" and "la" in the past 12 months in the US
gtrends_data_only_interest <- gtrends(keyword = c("nyc", "la"), geo = "US",  time = "today 12-m", onlyInterest = TRUE)

print(gtrends_data_only_interest)

low_search_volume

low_search_volume is another logical parameter that determines whether to include regions with low search volume. You set this to TRUE if want to include areas where not many people searched for the keyword and for false if you want to filter low-volume areas out of the data.

#Retrieving Google Trends data  for "nyc" and "la" over the past 12 months in the United States, including regions with low search volumes.
gtrends(keyword = c("nyc", "la"), geo ="US",  time = "today 12-m", low_search_volume = TRUE)

Important things to remember: When performing a Google Trends query, the function retrieves related topics that are commonly associated with the specific keywords; however, if you use more than one keyword, related topics will not be included in the results

The output of the gtrends function is an object of class ‘gtrends’, which contains a list of data frames derived from GTrends data.

usage- allows us to plot interests over time and you can do this by using the autoplot() function directly on gtrends for visualization of the trends.

gtrends(keyword = c("nyc", "la"), geo = "US", time = "today 12-m")
autoplot(gtrends_data)

Using the gtrends package in R provides valuable insights into public interest and search behavior regarding psychological topics, particularly in the context of emotion regulation and stress during the pandemic. Additionally, categorizing these searches by specific topics, using the keyword, allows for a deeper understanding of what individuals are seeking. This gtrend package allows to observe specifics such as geography, language, date, as well as interests can be specified by region. An example of how this could be handled is by searching stress depending on region and we can see whether stress is a top search in city compared to more rural parts of the US. GTrends also allows us to observe human behavior in real time and observe the popularity of it while also filtering things such as low interest.