Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
RStudio for R Statistical Computing Cookbook

You're reading from   RStudio for R Statistical Computing Cookbook Over 50 practical and useful recipes to help you perform data analysis with R by unleashing every native RStudio feature

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781784391034
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrea Cirillo Andrea Cirillo
Author Profile Icon Andrea Cirillo
Andrea Cirillo
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Acquiring Data for Your Project 2. Preparing for Analysis – Data Cleansing and Manipulation FREE CHAPTER 3. Basic Visualization Techniques 4. Advanced and Interactive Visualization 5. Power Programming with R 6. Domain-specific Applications 7. Developing Static Reports 8. Dynamic Reporting and Web Application Development Index

Getting data from Google Analytics

Google Analytics is a powerful analytics solution that gives you really detailed insights into how your online content is performing. However, besides a tabular format and a data visualization tool, no other instruments are available to model your data and gain more powerful insights.

This is where R comes to help, and this is why the RGoogleAnalytics package was developed: to provide a convenient way to extract data from Google Analytics into an R environment.

As an example, we will import data from Google Analytics into R regarding the daily bounce rate for a website in a given time range.

Getting ready

As a preliminary step, we are going to install and load the RGoogleAnalytics package:

install.packages("RGoogeAnalytics")
library(RGoogleAnalytics)

How to do it...

  1. The first step that is required to get data from Google Analytics is to create a Google Analytics application.

    This can be easily obtained from (assuming that you are already logged in to Google Analytics) https://console.developers.google.com/apis.

    After creating a new project, you will see a dashboard with a left menu containing among others the APIs & auth section, with the APIs subsection.

    After selecting this section, you will see a list of available APIs, and among these, at the bottom-left corner of the page, there will be the Advertising APIs with the Analytics API within it:

    How to do it...

    After enabling the API, you will have to go back to the APIs & auth section and select the Credentials subsection.

    In this section, you will have to add an OAuth client ID, select Other, and assign a name to your app:

    How to do it...

    After doing that and selecting the Create button, you will be prompted with a window showing your app ID and secret. Take note of them, as you will need them to access the analytics API from R.

  2. In order to authenticate on the API, we will leverage the Auth() function, providing the annotated ID and secret:
    ga_token ← Auth(client.id = "the_ID", client.secret = "the_secret")

    At this point, a browser window will open up and ask you to allow access permission from the app to your Google Analytics account.

    After you allow access, the R console will print out the following:

    Authentication complete
  3. This last step basically requires you to shape a proper query and submit it through the connection established in the previous paragraphs. A Google Analytics query can be easily built, leveraging the powerful Google Query explorer which can be found at https://ga-dev-tools.appspot.com/query-explorer/.

    This web tool lets you experiment with query parameters and define your query before submitting the request from your code.

    The basic fields that are mandatory in order to execute a query are as follows:

    • The view ID: This is a unique identifier associated with your Google Analytics property. This ID will automatically show up within Google Query Explorer.
    • Start-date and end-date: This is the start and end date in the form YYYY-MM-DD, for example, 2012-05-12.
    • Metrics: This refers to the ratios and numbers computed from the data related to visits within the date range. You can find the metrics code in Google Query Explorer.

    If you are going to further elaborate your data within your data project, you will probably find it useful to add a date dimension ("ga:date") in order to split your data by date.

    Having defined your arguments, you will just have to pack them in a list using the init() function, build a query using the QueryBuilder() function, and submit it with the GetReportData() function:

    query_parameters <- Init(start.date = "2015-01-01",
                             end.date   = "2015-06-30",
                             metrics    =   "ga:sessions,
                                             ga:bounceRate",
                             dimensions = "ga:date",
                             table.id = "ga:33093633")
    ga_query <- QueryBuilder(query_parameters)
    ga_df <- GetReportData(ga_query, ga_token)
    

    The first representation of this data could be a simple plot of data that will result in a representation of the bounce rate for each day from the start date to the end date:

    plot(ga_df)
    

There's more...

Google Analytics is a complete and always-growing set of tools for performing web analytics tasks. If you are facing a project involving the use of this platform, I would definitely suggest that you take the time to go through the official tutorial from Google at https://analyticsacademy.withgoogle.com.

This complete set of tutorials will introduce you to the fundamental logic and assumptions of the platform, giving you a solid foundation for any of the following analysis.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image