Course Context: This tutorial is designed for marketing students learning how to leverage publicly available search data for consumer insights, competitive intelligence, and campaign planning.
GitHub: github.com/utjimmyx


2 Required Packages

We will use the following R packages. Install any you don’t already have.

# Run once to install
install.packages(c("gtrendsR", "tidyverse", "ggplot2", "scales",
                   "lubridate", "knitr", "kableExtra"))
library(gtrendsR)     # Pull data from Google Trends
library(tidyverse)    # Data manipulation and plotting
library(ggplot2)      # Visualization
library(scales)       # Axis formatting
library(lubridate)    # Date handling
library(knitr)        # Tables
library(kableExtra)   # Enhanced table formatting

3 Understanding the gtrends() Function

The core function in gtrendsR is gtrends(). Here are its key arguments:

Argument Description Example
keyword Search term(s), up to 5 at a time c("Nike", "Adidas")
geo Country or region code (blank = worldwide) "US", "CA", "GB"
time Time range of the query "today 12-m", "2020-01-01 2024-12-31"
gprop Search property "web", "youtube", "news"
category Restrict to a Google topic category (0 = all) 0

Common time values:

"now 1-H"        → Past hour
"now 4-H"        → Past 4 hours
"today 1-m"      → Past 30 days
"today 3-m"      → Past 90 days
"today 12-m"     → Past 12 months
"today+5-y"      → Past 5 years
"all"            → Since 2004

4 Let’s test to see if the gtrendsR package works normally (see my notes at the beginning)

# test <- gtrends("coffee")

5 Tutorial 2: Competitive Comparison — Fast Food Chains

A common marketing use case is brand benchmarking. Let’s compare search interest for four major fast-food brands.

brands <- c("McDonald's", "Burger King", "Wendy's", "Chick-fil-A")

brand_trend <- gtrends(
  keyword = brands,
  geo     = "US",
  time    = "today+5-y"
)

brand_iot <- brand_trend$interest_over_time

# Convert hits to numeric (sometimes returned as character with "<1")
brand_iot <- brand_iot |>
  mutate(hits = as.numeric(ifelse(hits == "<1", "0.5", hits)))
brand_colors <- c(
  "McDonald's"  = "#FFC300",
  "Burger King" = "#D62300",
  "Wendy's"     = "#E2003A",
  "Chick-fil-A" = "#E4002B"
)

ggplot(brand_iot, aes(x = date, y = hits, color = keyword)) +
  geom_smooth(method = "loess", span = 0.3, se = FALSE, linewidth = 1.2) +
  scale_color_manual(values = brand_colors) +
  scale_x_datetime(date_labels = "%Y", date_breaks = "1 year") +
  labs(
    title    = "Competitive Search Interest: Fast Food Brands",
    subtitle = "United States | Past 5 Years | Web Search",
    x        = NULL,
    y        = "Relative Interest (0–100)",
    color    = "Brand",
    caption  = "Source: Google Trends via gtrendsR | github.com/utjimmyx"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title    = element_text(face = "bold", size = 14),
    plot.subtitle = element_text(color = "gray50"),
    legend.position = "bottom"
  )

Discussion Question: Which brand shows the most consistent interest over time? Which shows the most volatility? What marketing events might explain sudden spikes?

6 References


Jimmy Zhenning Xu, Ph.D., AIS Doctor, Principal Consultant | github.com/utjimmyx