################################################################################
##        Query Google Trends using R - your new digital marketing secret weapon
##        by Dr. Jimmy (Zhenning) Xu, 
##        follow me on Twitter https://twitter.com/MKTJimmyxu
################################################################################

What is Google Trends?

Google Trends is a very useful service for finding what’s popular among users of the Search engine and generating insights. Trending searches by Google users can be used as an information source that could perhaps improve the forecasts. In Google Trends, Google compiles and aggregates its search data, reflecting the interestin certain keywords or concepts.

In the marketing industry, the volume of queries made by users about the products via the search engine could reflect the potential volume of sales of these products. These data could therefore be considered as indicators of consumer purchase intention, both for manufactured goods and for services.

Why use R?

Using Google Trends manually is fun. However, it could be time-consuming and tedious if you would like to compare different search terms over different time frames.

In this tutorial, I introduce the tool by accessing it directly through a web browser to extract data and analyze it in R.  In particular, the main package used will be “gtrendsR” (intro to this package available at https://github.com/PMassicotte/gtrendsR)

Have fun!

library(gtrendsR)
library(maps)
library(ggplot2)
library(lettercase)
## Loading required package: stringr
## lettercase-0.13.1 - Copyright © 2018 Decision Patterns
library(viridis)
## Loading required package: viridisLite
library(pals)
## 
## Attaching package: 'pals'
## The following objects are masked from 'package:viridis':
## 
##     cividis, inferno, magma, plasma, viridis
## The following objects are masked from 'package:viridisLite':
## 
##     cividis, inferno, magma, plasma, viridis
library(ggrepel)
library(tidyverse)
## -- Attaching packages --------------------------------------------------------- tidyverse 1.2.1 --
## v tibble  1.4.2     v purrr   0.2.5
## v tidyr   0.8.1     v dplyr   0.7.6
## v readr   1.1.1     v forcats 0.3.0
## -- Conflicts ------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x purrr::map()    masks maps::map()
my_theme <- function() {
  theme_bw() +
    theme(panel.background = element_blank()) +
    theme(plot.background = element_rect(fill = "seashell")) +
    theme(panel.border = element_blank()) +                     # facet border
    theme(strip.background = element_blank()) +                 # facet title background
    theme(plot.margin = unit(c(.5, .5, .5, .5), "cm")) +
    theme(panel.spacing = unit(3, "lines")) +
    theme(panel.grid.major = element_blank()) +
    theme(panel.grid.minor = element_blank()) +
    theme(legend.background = element_blank()) +
    theme(legend.key = element_blank()) +
    theme(legend.title = element_blank())
}

#product trends
trend <- gtrends(c("laptop","mobile","tablet"))

#which player receives the most attention after Nick's 30th aniversary campaign?
games<- gtrends(c("Serena Williams", "Colin Kaepernick"), 
                geo = c("US", "CA"))

#Which hurricane received the most attention?

hurricanes1 <- gtrends(c("harricane Maria", 
                        "harricane Katrina","harricane Harvey", 
                        "hurricane Florence", "hurricane michael"), time = "all", gprop = "web", geo = c("US"))

#Which hurricane received the most attention?

hurricanes2 <- gtrends(c("hurricane Florence", "hurricane michael"), time = "2018-09-01 2018-10-14", gprop = "web", geo = c("US"))

#Which party in Texas received the most attention?
midterm1 <- gtrends(c("Beto O'Rourke", "Ted Cruz"), time = "2018-09-01 2018-10-10", 
                    gprop = "web", geo = c("US-TX"))

midterm2 <- gtrends(c("Democrats", "Republicans"), time = "2018-09-01 2018-10-10", 
                    gprop = "web", geo = c("US-TX"))

midterm3 <- gtrends(c("blue wave", "red tide"), time = "2018-01-01 2018-10-10", 
                    gprop = "web", geo = c("US"))

Plots

Plotting the quries below, we note the contrast between different terms in different places. For search marketers, you can use this to help with keyword research by discovering valuable insights about targeted keywords.

You can use this for predicting things that might happen, but you can also use it to monitor changes in other things that are related loosely.

It is interesting to see that Serena Williams is even more popular than Colin Kaepernick even after Nick’s Colin Kaepernick’s Nike deal.

plot(trend)

plot(games)+my_theme() +
  geom_line(size=1.0)

plot(hurricanes1) + 
  my_theme() +
  geom_line(size=.7)

plot(hurricanes2) + 
  my_theme() +
  geom_line(size=.5)

plot(midterm1) +
  my_theme() +
  geom_line(size=0.5)

plot(midterm2) +
  my_theme() +
  geom_line(size=0.5)

plot(midterm3) +
  my_theme() +
  geom_line(size=0.5)

References: package #gtrendR https://github.com/PMassicotte/gtrendsR

Geo ccoding: https://github.com/PMassicotte/gtrendsR/issues/213 https://stackoverflow.com/questions/50318748/gtrendsr-geo-msa-area-code