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 1: Single Keyword — Brand Search Interest

Let’s start simple: track search interest for “Starbucks” in the United States over the past 12 months.

# Query Google Trends
starbucks_trend <- gtrends(
  keyword = "Starbucks",
  geo = "US",
  time = "today+5-y"
)

# Extract interest over time
iot <- starbucks_trend$interest_over_time
ggplot(iot, aes(x = date, y = hits)) +
  geom_line(color = "#00704A", linewidth = 1) +
  geom_smooth(method = "loess", se = TRUE, color = "#1E3932",
              fill = "#D4E9E2", alpha = 0.3) +
  scale_x_datetime(date_labels = "%b %Y", date_breaks = "2 months") +
  labs(
    title    = "Google Search Interest: Starbucks",
    subtitle = "United States | Past 5 years | Web Search",
    x        = NULL,
    y        = "Relative Interest (0–100)",
    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"),
    axis.text.x   = element_text(angle = 30, hjust = 1)
  )

Marketing Insight: Look for dips and spikes. Dips may correspond to off-peak seasons or PR crises. Spikes often align with new product launches, promotions, or viral social media moments.

6 References


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