Learn R Programming

shinyga (Shiny Google Authentication)

Easier Google Authentication Dashboards in Shiny.

The functions in the package were used to help create these demo apps:

Get in touch if you have any dashboards made with this package to get a link from here

!Deprecated!

I would now recommend that for Shiny apps using Google Analytics to use googleAnalyticsR as it has more features and is compatible with other Google APIs written with googleAuthR.

No new features will be added to shinyga()

Change history

Dev version

  • Integrating googlesheets authentication
  • GA users.

Version 0.1.2 - May 25th 2015

  • Added GA filters, custom metrics, Adwords API listings

Version 0.1.1 - March 27th 2015

  • Fixed bug where older GA accounts couldn't fetch segments or goals.
  • Port everything to httr, get rid of RCurl, RJSONIO
  • Segments will default to default GA ones if it can't find or parse yours
  • Removed need for redirect URL in Auth macro, Shiny will detect App URL from session info.

Version 0.1.0 - March 18th 2015

  • Initial Release

Help

Please tell me any bugs or problems with examples or documentation in the issue tracker

Most functions have help files, start with ?shinyga

The package focused on quick setup of Google Authentication APIs, with initial focus on Google Analytics, so please keep that in mind when asking for features.

What shinyga does

Provides utility functions to help easily setup up a Google API authentication flow in Shiny. The focus is on Google Analytics initially, with macros to help easy download of GA data, but to be expanded later into other Google APIs such as Google Tag Manager, Gmail, BigQuery etc.

Installation

library(devtools)

## install shinydashboard dependency
devtools::install_github("rstudio/shinydashboard")

## install shinyga
devtools::install_github("MarkEdmondson1234/shinyga")

library(shiny)
library(shinydashboard)
library(shinyga)

?shinyga

To Use

Get Google API credentials

Get your client secret, ID from the Google API console

  • Create a Project
  • Go to APIs & Auth - Activate the appropriate Google API (only necessary for Analytics atm)
  • Go to APIs & Auth - Credentials.
  • Create new Client ID for web application
  • Note your Client ID and Client secret
  • Put the URL of your app in the Redirect URIs, one per line. See below.
  • Fill in some details on your consent screen

Client URL: Running Locally

For local the Shiny runApp() uses a random port, so specify using runApp(port=1234) and put that in the Google API console as your port number e.g. http://127.0.0.1:1234

Client URL: Running on Shiny Server or Shinyapps.io

If you use the doAuthMacro() functions it will detect your app URL for you, otherwise you will need to specify it via the redirect.uri parameter in the underlying authentication functions.

Use the URL where your app is published as your CLIENT_URL. You can put both your local and live URL in the Google API console.

TIP: Comment out the local one when you are ready to deploy. e.g. https://mark.shinyapps.io/ga-effect/

Run Shiny

Read how to use Shiny apps before using this package.

This package also uses shinydashboard to make the pretty layout, but its not strictly necessary.

Shiny App Code Examples

Examples of minimal working examples are shown below, for you to adapt.

Google Analytics

###### server.r

library(shiny)
library(shinydashboard)
library(shinyga)

securityCode <- createCode()

shinyServer(function(input, output, session){

  ## returns list of token and profile.table ----------------------------------
  auth <- doAuthMacro(input, output, session,
                      securityCode,
                      ## client info taken from Google API console.
                      client.id     = "xxxxx.apps.googleusercontent.com",
                      client.secret = "xxxxxxxxxxxx",
                      type = "analytics")

  ## auth returns auth$table() and auth$token() to be used in API calls.

  ## get the GA data ----------------------------------------------------------
  gadata <- reactive({
    validate(
      need(auth$token(), "Authentication needed"))

    df    <- auth$table()
    token <- auth$token()
    gaid  <- as.character(input$view)
    
    profileRow <- df[df$id %in% gaid,] 
    
    data <- rollupGA(GAProfileTable = profileRow,
                     dimensions = 'ga:date',
                     start_date = '2014-09-01',
                     metrics = 'ga:sessions',
                     end_date = '2015-03-01',
                     ga = token)
                     
    data[,c('date','sessions')]
  }) 


  ## do a plot! ---------------------------------------------------------------
  output$gaplot <- renderPlot({
    validate(
      need(gadata(), "Authenticate to see"))

    plot(gadata(), type="l") 

    })

})


##### ui.r

library(shiny)
library(shinydashboard)
library(shinyga)

dashboardPage(
  dashboardHeader(
    title = "Shiny Google Authentication demo",
    dropdownMenuOutput("messageMenu")
  ),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Tab1", tabName = "setup", icon = icon("gears")),
      menuItem("Tab2", tabName = "dash", icon = icon("dashboard")))
    ),
  dashboardBody(
    uiOutput("AuthGAURL"),
    authDropdownRow(),
    plotOutput("gaplot")
  )
)

Copy Link

Version

Version

0.1.2.9001

License

MIT

Maintainer

Mark Edmondson

Last Published

February 15th, 2017

Functions in shinyga (0.1.2.9001)

doSegmentMacro

Quick setup of shinyga segments
MEgetData

MEgetData - does the actual fetch of GA data, called from rollupGA
authReturnCode

Returns the authentication parameter "code" in redirected URLs
addMessageData

addMessageData - creates another Message object
doAuthMacro

Quick setup of shinyGA authentication
getAndMergeGAAccounts

getAndMergeGAAccounts - creates dataframe of all GA accounts/webproperties/views
metricSelect

metricSelect - creates a selection of which metric
authDropdownRow

authDropdownRow - creates a row of GA authentication menus
initMessageData

initMessageData - creates Message object
createCode

Creates a random character code
shinygaGetCustomMetrics

Get GA customMetrics
shinygaGetAccounts

Get GA Account data
processManagementData

Take GA API output and parses it into data.frame
shinygaGetCustomDataSources

Get GA customDataSources
shinygaGetCustomDimensions

Get GA customDimensions
shinyga

shinyga: Easier Google Analytics Dashboards in Shiny.
renderAuthDropdownRow

renderAuthDropdownRow - creates a row of GA authentication menus
shinygaGetAdWords

Get GA AdWords links
rollupGA

rollupGA - get GA data from multiple Views
shinygaGetFilters

Get GA Filters
shinygaGetProfiles

Get GA View data
shinygaGetGoals

Get GA Goals
shinygaGetWebProperties

Get GA Web Property data
shinygaGetSegments

Get GA Segments
shinygaGetTokenURL

Returns the authentication URL
shinygaGetToken

Returns the authentication Token
shinygaGetUsers

Get GA Users