Introduction to R for Marketers

R is a scripting language used by Statisticians, Analysts, Data Scientists and alike. Though it can also be used by Marketers in order to extract, analyse or automate marketing data.

There are two ways in which you can run R

1) Locally (you install R Console and then R Studio)

2) on Cloud, using R Studio Cloud (aka Posit). In this case no installation is required.

Both options mentioned above are available for anyone for free.

R can be a very useful tool or Digital Marketers, and you really do not need to be an analyst or a Data Scientist in order to use it.

Some of the things you can use R for are:

  • Extracting data (e.g. from Google Ads, Search Ads 360, Google Analytics, Google Trends, Facebook, Search console and more)

  • Building reports (e.g. Blended Search report based on Google Ads and Search Console data)

  • Analysis (e.g. Marketing Mix Modelling)

  • Data visualisation

Examples of how to use R for Marketing

Below you can find several quick examples showing you how you can use R for Marketing. We strongly encourage you to give it a go. You do not need any special software to be installed. All you need it to register for a free account on R Studio Cloud and try some of the code samples below

Extract Google Search Console data using R

Sample code below extracts Query level data from Google Search Console. Data is then saved as a CSV, Excel, or directly into Google Sheets.

##### Extracting Google Search Console data with R #####
##### AnalyticsHacker, 2023 #####

# Required libraries
install.packages("searchConsoleR")
library(searchConsoleR)

## Authentication to Google Search Console ##
# If you run this script on your local machine, you do not need
# a token file, otherwise you will need to generate a token. The
# only reliable way to generate a token is by running this code
# in your local machine, however the Google OAuth 2.0 page should
# be helpful to generate a token without a local machine
scr_auth(token = 'YOUR-TOKEN-HERE')

# This command will list all the websites associated with your account
list_websites()

# Parameters of search
website <- "https://www.example.com/" # One of the websites from the previous command.
start_date <- '2023-01-01'
end_date <-'2023-03-01'
dimensions <- c("query", "device") # query, device, date or a combination of them can be used
dimensionFilters <- c('country==gbr') 
aggregation <- c("auto") # auto, byPage, byProperty

# Fetching the Dataset
queries <- search_analytics(website,
                            startDate = start_date,
                            endDate = end_date,
                            dimensions = dimensions, 
                            dimensionFilterExp = dimensionFilters,
                            aggregationType = aggregation,
                            prettyNames = TRUE,
                            walk_data = "byBatch")

# Saving the data

############ Option 1: Export as CSV ############
write.csv(queries, "queries.csv")

############ Option 2: Export as Excel ############
install.packages("writexl") # We need an extra library for this
library(writexl)
write_xlsx(queries,"queries.xlsx")

############ Option 3: Export as Google Sheets ############
# This option requires you to have a spreadsheet already created
# And with full access.
install.packages("googlesheets4") 
install.packages("googledrive")
library(googledrive)
library(googlesheets4)

# Authentication
drive_auth(use_oob = TRUE, cache = TRUE)
gs4_auth(token = drive_token())

# Replace this URL with your own Spreadsheet
SPREADSHEET = "https://docs.google.com/spreadsheets/d/<ID>/edit"

# Replace this with the name of the sheet in your destination spreadsheet
SHEET = "<NAME OF SHEET>"

# Write to sheet
write_sheet(queries, ss=SPREADSHEET, sheet=SHEET)

Extract Google Trends data using R

Sample code below extracts data from Google Trends (either as a set of 5 keywords which can be directly compared, or any unlimited number of keywords though these should then not be directly compared to each other).

The data is the saved into a CSV, Excel or Google Sheets.

##### Extracting Google Trends data with R #####
##### AnalyticsHacker, 2023 #####
#
# WARNING: Sometimes while running a request, specially in option 1,
# Google can deny it and it will throw an error. You can wait a couple
# of minutes and retry again, and it should work.
# 
# The error could either be a status code 429 or a widget$status code == 200 not true

install.packages("gtrendsR")
install.packages("dplyr")
install.packages("plyr")
install.packages("data.table")
install.packages("sqldf")
install.packages("lubridate")
library(lubridate)
library(sqldf)
library(data.table)
library(plyr)
library(dplyr)
library(gtrendsR)

# Setting up global parameters
time_period = "today+5-y"
geo=c("GB")

####### OPTION 1: Exporting unlimited keywords, without comparison #######
#
# For this part, you just have to replace the keyword argument for each individual
# keyword. You can add another request, by copying and pasting request lines and
# changing the number. The pattern for a request should be:
#
#requestXX <- data.frame(gtrends(keyword="<insert keyword here>", time=time_period,geo=geo)$interest_over_time)

request01 <- data.frame(gtrends(keyword='keyword01', time=time_period,geo=geo)$interest_over_time)
request02 <- data.frame(gtrends(keyword='keyword02', time=time_period,geo=geo)$interest_over_time)
#<Insert more requests here>#

reqlist=ls(pattern="^request");
all_keywords <- data.frame(ldply(mget(list), data.frame),type="individual keyword")
gtrends_data <- data.frame(all_keywords, week_ending = as.Date(all_keywords$date)+6)

####### OPTION 2: Exporting up to 5 comparable keywords #######
# time period and geo data should be set in the global parameters!

keywords = c("keyword01", "keyword02", "keyword03") # Just add or remove keywords as you desire

gtrends_data <- data.frame(gtrends(keyword=keywords,time=time_period,geo=geo)$interest_over_time)

#################### EXPORTING DATA ####################

############ Option 1: Export as CSV ############
write.csv(gtrends_data, "gtrends_data.csv")

############ Option 2: Export as Excel ############
install.packages("writexl") # We need an extra library for this
library(writexl)
write_xlsx(gtrends_data,"gtrends_data.xlsx")

############ Option 3: Export as Google Sheets ############
# This option requires you to have a spreadsheet already created
# And with full access.
install.packages("googlesheets4") 
install.packages("googledrive")
library(googledrive)
library(googlesheets4)

# Authentication
drive_auth(use_oob = TRUE, cache = TRUE)
gs4_auth(token = drive_token())

# Replace this URL with your own Spreadsheet
SPREADSHEET = "https://docs.google.com/spreadsheets/d/<ID>/edit"

# Replace this with the name of the sheet in your destination spreadsheet
SHEET = "<NAME OF SHEET>"

# Write to sheet
write_sheet(gtrends_data, ss=SPREADSHEET, sheet=SHEET)

Extract Google Analytics data using R

Sample code below extracts data from Google Analytics. The data is the saved into a CSV file.

# run one section at a time (sections are separated with a semi colon)
# to run a section, first select the section (with a mouse or keyboard) and then either hit the Enter+Command button, or click on "Run"


install.packages(googleAnalyticsR);
library(googleAnalyticsR);

#authentication
# option 1, 
# type below the email address that you use to access Google Analytics; then run the section (that will open a browser); then select appropriate options in the browser window; 
ga_auth(token = NULL, email = "youremailaddress@gmail.com", json_file = NULL);


# See a full list of the views that you have access to
my_accounts <- ga_account_list();


#authentication
# option 2, 
# options(googleAuthR.verbose=2)
#client_id <- 'XXX'     #replace XXX with client id which you can obtain (for free) from Google Cloud Platform
#client_secret <-  'XXX'  #replace XXX with client secret which you can obtain (for free) from Google Cloud Platform

#################################################################################


# set viewID (replace XXX with your Google Analytics view id)
ga_id <- "ga:XXX";

## prepare filters on dimensions
nondublin <- dim_filter("city", "EXACT", "Dublin",not = TRUE);   #this excludes Dublin
browser_filter <- dim_filter("browser", "REGEXP", "samsung",not = TRUE);  #this excludes browser Samsung

## construct filter objects (from the objects your created above)
fc <- filter_clause_ga4(list(nondublin,browser_filter), operator = "AND");

# set date range
dates <- seq(as.Date("2023-01-01"), as.Date("2023-03-15"), by=1);



ga_raw<- google_analytics(ga_id 
                # date_range = c("2021-08-01", "2022-12-31"), 
                 , date_range = c(thedate, thedate)
                 , metrics = c("sessions" ,"transactions")   #you can add more metrics if yuo want
                 , dimensions = c("date","deviceCategory","channelGrouping","Source","country","landingpagepath")
                 , dim_filters = fc
                 , anti_sample = TRUE   #this is to avoid sampling
                 );


#save output to a csv file (it will save to you present working directly, but you can change the location in the file option if needed)
# to check your working directly simply run --> getwd()
# to set your working directly to Desktop simply run --> setwd("~/Desktop")
try(write.table(ga_raw,  file = 'googleanalytics-data.csv' ,append=FALSE,row.names=FALSE, na="",col.names=TRUE, sep=","));

# END loop


#OPTIONAL

## if you want you can lowercase all content of the output (and then save as csv)
ga_raw_lower <- data.frame(lapply(ga_raw, function(v) {
  if (is.character(v)) return(tolower(v))
  else return(v)
}));

#save lower-cased output to a csv file
write.table(ga_raw_lower,  file = 'googleanalytics-data.csv' ,append=FALSE,row.names=FALSE, na="",col.names=TRUE, sep=",");