Recently I have been playing a lot with Tableau, and I decided to extract data from my Google Analytics Account in regard to one of my blogs and visualize the extracted data. Today I want to share my findings. Every visualization gives us some insights into the data. Data connection with Google Analytics opens pretty quick. You can select up 7 dimensions and 10 measures to work with.
The blog is about traveling, and it’s in Russian. Its main audience is in Russia, Eastern Europe, and United States. Below you can see number of sessions, pageviews, and new users by country and year quarter as well as a map.
The bubble and waterfall charts below is basically showing the same information, but I like how they look 🙂 The waterfall chart shows the number of organic searches by country. We can see that although the U.S. is the third country by number of pageviews, it’s the second country by the number of organic searches. And Belarus where I am from is the second by number of pageviews, but the forth by the number of organic searches. I guess my friends and family from Belarus influenced this result.
You will need RGoogleAnalytics package to extract Google Analytics data in R. The package was developed by Michael Pearmain, and it provides functions for accessing and retrieving data from the Google Analytics API. This article is based on the package’s supporting documentation. To download the documentation use this link: RGoogleAnalytics documentation.
First, install the package RgoogleAnalytics. It requires the packages “lubridate” and “httr” to be installed as well.
If you have problems with downloading the packages, check your R version. RGoogleAnalytics requires R version 3.0.2 or newer.
Then, you will use the Auth function to authorize the RGoogleAnalytics package to your Google Analytics Account using Oauth2.0.
The function Auth expects a Client ID and Client Secret. To get these, you will have to register an application with the Google Analytics API:
1. Go to the Google Developers Console
2. Create a New Project and enable the Google Analytics API
3. On the Credentials screen, create a new Client ID for Application Type “Installed Application”
4. Copy the Client ID and Client Secret to your R Script
Enable Google Analytics API
Create a new Client ID
Now you can authorize the RGoogleAnalytics package to your Google Analytics Account.
I would like to create “The most popular sold product per day” report for the last 30 days. The header of the report should look like the picture below. We need the Date, Product SKU number, Product name, Product Brand and Quantity Sold. The most popular product is the product sold the maximum number of times on that day.
SELECT CreatedAt, sku AS SKU, name AS Description,
eaov.value AS Brand, MAX( qty_total ) AS Qty
SELECT DATE( created_at ) AS CreatedAt, sku,
SUM( qty_ordered ) AS qty_total, name, product_id
FROM `sales_flat_order_item` AS sfoi
WHERE sfoi.created_at > DATE_SUB( CURDATE( ) , INTERVAL 360 DAY )
GROUP BY sku, CreatedAt
ORDER BY qty_total DESC
) AS item_count
/* get brand name*/
LEFT JOIN `catalog_product_entity_int` AS cpei ON cpei.attribute_id =81
AND cpei.entity_id = item_count.product_id
LEFT JOIN `eav_attribute_option_value` AS eaov ON eaov.option_id = cpei.value
GROUP BY CreatedAt
Disadvantage: if two product are equally popular, only one of them is chosen for the report.
The CURDATE() function returns the current date.
The NOW() function returns the current system date and time.
If you want to get a sales report for the last X days, make sure to think first what kind of output you want to get.
For me, if I run the same sales report for the last X days in the morning and in the evening of the same day, I want to get the same report without regard the current time, so I will use the CURDATE() function.
In case if we use the NOW() function, and two people want to generate the same report and then do something with it, each of them will get a slightly different version of the report since it’s nearly impossible to run the SQL query at the same time by two different people. It will create discrepancies. Example: Orders report for the last X days
SELECT SFO.increment_id AS OrderId, SFO.base_subtotal AS BasketAmount,
SFO.base_shipping_amount AS Shipping,
SFO.base_discount_amount AS Discount,
SFO.base_tax_amount AS Tax,
IFNULL( SFO.base_total_paid, 0 ) AS CustomerPaid
FROM `sales_flat_order` AS SFO
SFO.created_at > DATE_SUB( CURDATE( ) , INTERVAL X DAY )
ORDER BY SFO.created_at DESC
SELECT sfoi.sku AS SKU, sum(qty_ordered) AS Qty , name AS Description
FROM `sales_flat_order_item` AS sfoi
JOIN `catalog_product_entity` AS cpe ON cpe.entity_id = sfoi.product_id
GROUP BY product_id
ORDER BY sum(qty_ordered) DESC
However this query does not address the situation when we have more than five most popular product with the same number of units sold. If we have two products that were sold equal number of times, ‘Top Product Number Five’ and ‘Top Product Number 6’, MySQL will return either the ‘Top Product Number Five’ or the ‘Top Product Number 6’ randomly for the fifth most popular product each time we run the query. View on Github
The dataset has to be downloaded in your working directory. Getwd() funcion returns an absolute filepath representing the current working directory of the R process. To change your working directory in R you need to use setwd(dir) function or go to the File menu in the R Cosole and choose “Change dir”.
fileUrl < - "your link here"
download.file(fileUrl,destfile = "./salaries/computer.xls")
Next step is to install the xlsx R package if you have not done so previously. To install xlsx, use install.packages(“xlsx”), to ensure if you have it or no, enter find.package(“xlsx”) in the console. After xlsx is done installing, load it using library(xlsx).
All three options above are the same, we are choosing a certain column. For example, if we have a data frame survey which consists of 1,000 observations, and each observation is described by 3 variables: gender, age, and marital status, by using survey$age we can subset the column named “age” for all 1,000 observations.