Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

no applicable method for 'as.request' applied to an object of class "function" #55

Open
simran-k opened this issue Jun 27, 2018 · 1 comment

Comments

@simran-k
Copy link

simran-k commented Jun 27, 2018

This is error message I get from time to time:

Fetch data by day: from 2018-06-26 to 2018-06-26.

  |                                                                            
  |                                                                      |   0%Error in UseMethod("as.request") : 
  no applicable method for 'as.request' applied to an object of class "function"
Calls: tapply ... get_data -> api_request -> GET -> request_build -> as.request

The error has been reported here before as well. My packages are all updated. If this package is not being maintained anymore, please let me know but such problems end up breaking our data pipeline in production cluster and it's critical to resolve this.

Through the cron job, it was attempted twice for execution, but both of the attempts were unsuccessful.

The script executes through a cron job and here's the script for your reference:

# Load up the RGA package. This is the package that has the smarts to actually
# connect to and pull data from the Google Analytics API
Sys.setenv(HADOOP_CMD="/usr/bin/hadoop")
library(RGA)
library(rhdfs)
#Authentication
#options(httr_oob_default=TRUE)


#Insert the client id retrieved fro4/4NvmCy2PuyN7vvywVywuabCWaZzApdmThN0fSfgnOjQm the Google developer #console
client.id <- 'CLIENT_ID'

#Insert the client secret retrieved from the Google developer #console
client.secret <- 'CLIENT_SECRET'

viewIds <- c( "ga:62814012","ga:124135466", "ga:132796646", "ga:131158887");

yesterdayDate <- Sys.Date() -1;
yesterdayDate <- as.character.Date(yesterdayDate)
print("YESTERDAY DATE")
print(yesterdayDate)

fileNames <- c();
print("going to authorize")
token <- authorize(client.id = "962653212381-l7bpufol4vbpvtruvh6sst84hru4on27.apps.googleusercontent.com", client.secret ="h6XYPURGo331v_NKmC16yU42", cache = "/home/rstudio/gaToken")

print("token in ga")
print(token)
maxDate <-"2017-07-01";


getLastImport = function(){
  if(file.exists("/home/rstudio/maxDateGAConsolidated")){
    mDate <- read.table("/home/rstudio/maxDateGAConsolidated", header=FALSE)
    print(mDate$V1)
    maxDate <<- mDate$V1
    #maxDate <<- as.Date(maxDate) + 1;
    print("max date is")
    print(maxDate)
    print("yesterday date is")
    print(yesterdayDate)
    if(maxDate >=  as.Date(yesterdayDate)){
      maxDate <<- yesterdayDate 
      stop(simpleError("Data has already been updated. You were trying to duplicate the data"))
    }else{
      maxDate <<- as.Date(maxDate) + 1;
      return(maxDate)
    }
  }else{
    print("max date dpes nt exist")
    maxDate <<- as.Date(maxDate);
    print("max date after increment")
    print(maxDate)
    return(maxDate)
  }
}
getLastImport()


print(maxDate)

fetchData <- function(id) {
  print("fetching data")
  print("id")
  print(id)
  print("max date in fetch data")
  print(maxDate)
  assign(paste0("gaDataUser", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:transactions", "ga:users", "ga:newUsers","ga:percentNewSessions", "ga:sessionsPerUser"),dimensions = c("ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  print("firstfetch")
  assign(paste0("gaDataSession", substring(id, 4)) , get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:sessions", "ga:bounces", "ga:bounceRate", "ga:sessionDuration", "ga:avgSessionDuration", "ga:hits"),dimensions = c("ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by = "day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDataSources", substring(id, 4)) , get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:transactions", "ga:organicSearches"),dimensions = c("ga:transactionId", "ga:fullReferrer", "ga:campaign",  "ga:sourceMedium", "ga:keyword"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDataEventTracking", substring(id, 4)) , get_ga(profileId =id, start.date = maxDate,end.date = "yesterday" , metrics = c( "ga:totalEvents", "ga:uniqueEvents", "ga:eventValue", "ga:avgEventValue", "ga:sessionsWithEvent", "ga:eventsPerSessionWithEvent"),dimensions = c("ga:eventCategory", "ga:eventAction", "ga:eventLabel", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by = "day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDataEcommerce",substring(id, 4)) , get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:transactions",  "ga:transactionRevenue", "ga:revenuePerTransaction", "ga:transactionRevenuePerSession", "ga:transactionShipping", "ga:transactionTax", "ga:totalValue", "ga:itemQuantity"),dimensions = c("ga:transactionId", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by = "day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDataEcommerce2", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:transactions","ga:itemRevenue", "ga:productAddsToCart", "ga:productCheckouts", "ga:productListClicks", "ga:productListViews", "ga:quantityAddedToCart"),dimensions = c("ga:transactionId", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by = "day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDataEcommerce3", substring(id, 4)) ,get_ga(profileId = id, start.date =maxDate,end.date = "yesterday" , metrics = c("ga:quantityCheckedOut", "ga:productListCTR", "ga:productRevenuePerPurchase"),dimensions = c("ga:transactionId","ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by = "day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDataEcommerce4", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c( "ga:buyToDetailRate", "ga:cartToDetailRate", "ga:internalPromotionCTR", "ga:internalPromotionClicks", "ga:internalPromotionViews", "ga:uniquePurchases"),dimensions = c("ga:transactionId", "ga:date" ), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by = "day", token), envir = .GlobalEnv)
  
  assign(paste0("gaDeviceData", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:transactions"),dimensions = c("ga:transactionId", "ga:browser", "ga:operatingSystem"," ga:mobileDeviceBranding", "ga:mobileDeviceModel", "ga:mobileDeviceInfo", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("usersAndSessionsGrouped", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users", "ga:sessions"),dimensions = c("ga:date", "ga:sourceMedium"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("pageViewsGroupedByPath", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:entrances","ga:entranceRate","ga:pageviews","ga:pageviewsPerSession",  "ga:uniquePageviews", "ga:timeOnPage", "ga:avgTimeOnPage", "ga:exitRate"),dimensions = c("ga:pageTitle", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("adwordsCost", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:adCost"),dimensions = c("ga:date", "ga:deviceCategory"), sort = NULL, filters = "ga:sourceMedium==google / cpc;ga:campaign!@android;ga:campaign!@IOS;ga:campaign!@APPENG;ga:campaign!@UAC_ALL",segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("dataSourcesWithRevenue", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:transactions", "ga:organicSearches", "ga:transactionRevenue"),dimensions = c("ga:transactionId", "ga:fullReferrer", "ga:campaign",  "ga:sourceMedium"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("landingpage", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:pageviews", "ga:uniquePageviews", "ga:transactions", "ga:transactionRevenue"),dimensions = c("ga:landingPagePath", "ga:sourceMedium", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("campaignWithDevice", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:transactions", "ga:transactionRevenue","ga:organicSearches"),dimensions = c("ga:transactionId", "ga:orderCouponCode", "ga:fullReferrer", "ga:campaign",  "ga:sourceMedium", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
 
  assign(paste0("versioning_mobile", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users"),dimensions = c("ga:date", "ga:appVersion", "ga:screenName"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
   
  assign(paste0("versioning_withoutScreenNamemobile", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users"),dimensions = c("ga:date", "ga:appVersion"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("mobile_campaign", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:users", "ga:newUsers","ga:sessions", "ga:avgSessionDuration", "ga:transactionRevenue"),dimensions = c("ga:date","ga:campaign","ga:sourceMedium",  "ga:transactionId", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("mobile_campaign_noTransactionId", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c( "ga:users", "ga:newUsers","ga:sessions", "ga:avgSessionDuration", "ga:transactionRevenue"),dimensions = c("ga:date","ga:campaign", "ga:sourceMedium", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("hourlyScreenViews", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c( "ga:users"),dimensions = c("ga:date","ga:hour", "ga:screenName"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("UsersByHour", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c( "ga:users"),dimensions = c("ga:date","ga:hour"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("PDPByHour", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:users"),dimensions = c("ga:date","ga:hour","ga:screenName"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("impressions", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date  = "yesterday", metrics = c( "ga:productListViews","ga:productListClicks","ga:productListCTR","ga:productAddsToCart","ga:productCheckouts","ga:uniquePurchases"),dimensions = c("ga:productSku", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("pageBySourceMedium", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date  = yesterdayDate, metrics = c( "ga:uniquePageviews", "ga:avgTimeOnPage", "ga:pageviews","ga:entrances","ga:bounceRate","ga:exitRate","ga:pageValue"),dimensions = c( "ga:date", "ga:pagePath", "ga:sourceMedium"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("fastDispatch", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:bounceRate","ga:entrances", "ga:pageviews", "ga:uniquePageviews", "ga:avgTimeOnPage", "ga:exitRate"),dimensions = c("ga:date", "ga:pagePath", "ga:shoppingStage"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  if(id == "ga:124135466" | id == "ga:112973340"){
    print("in if")
    assign(paste0("visitsDataBySourceMediumAndDevice", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users", "ga:sessions", "ga:newUsers", "ga:bounces"),dimensions = c("ga:date", "ga:sourceMedium", "ga:operatingSystem"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  }else{
    print('in else')
    assign(paste0("visitsDataBySourceMediumAndDevice", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users", "ga:sessions", "ga:newUsers", "ga:bounces"),dimensions = c("ga:date", "ga:sourceMedium", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  }
  
  if(id == "ga:124135466" | id == "ga:132796646" | id == "ga:112973340"){
    assign(paste0("appTracking", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:screenviews", "ga:uniqueScreenviews", "ga:screenviewsPerSession", "ga:timeOnScreen", "ga:avgScreenviewDuration"),dimensions = c("ga:screenName", "ga:date", "ga:operatingSystem"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    assign(paste0("gaDataCampaignReportGroupDateCampaign", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:newUsers", "ga:users"),dimensions = c( "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    assign(paste0("gaDataCampaignReport", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:sessions","ga:newUsers","ga:percentNewSessions","ga:bounceRate","ga:avgSessionDuration","ga:pageviewsPerSession","ga:transactions","ga:transactionRevenue"),dimensions = c("ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    assign(paste0("pageViewsGroupedByPathSourceMedium", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:screenviews", "ga:uniqueScreenviews", "ga:screenviewsPerSession", "ga:timeOnScreen", "ga:avgScreenviewDuration"),dimensions = c("ga:screenName", "ga:date", "ga:operatingSystem"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
   # assign(paste0("versioning_mobile", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users"),dimensions = c("ga:date", "ga:appVersion", "ga:screenName"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  #  assign(paste0("versioning_withoutScreenNamemobile", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:users"),dimensions = c("ga:date", "ga:appVersion"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    
  }else{
    assign(paste0("appTracking", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:entrances","ga:entranceRate","ga:pageviews","ga:pageviewsPerSession",  "ga:uniquePageviews", "ga:timeOnPage", "ga:avgTimeOnPage", "ga:exitRate"),dimensions = c("ga:pagePath", "ga:date", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    assign(paste0("gaDataCampaignReportGroupDateCampaign", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:sessions","ga:newUsers", "ga:users"),dimensions = c("ga:campaign", "ga:date"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    assign(paste0("gaDataCampaignReport", substring(id, 4)), get_ga(profileId = id, start.date = maxDate ,end.date = "yesterday" , metrics = c("ga:sessions","ga:newUsers","ga:percentNewSessions","ga:bounceRate","ga:avgSessionDuration","ga:pageviewsPerSession","ga:transactions","ga:transactionRevenue"),dimensions = c("ga:transactionId","ga:campaign","ga:sourceMedium"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    assign(paste0("pageViewsGroupedByPathSourceMedium", substring(id, 4)) ,get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:entrances","ga:entranceRate","ga:pageviews","ga:pageviewsPerSession",  "ga:uniquePageviews", "ga:timeOnPage", "ga:avgTimeOnPage", "ga:exitRate"),dimensions = c("ga:pagePath", "ga:date", "ga:deviceCategory", "ga:sourceMedium"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
    
  }
  
  assign(paste0("banner_ecommerce", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c( "ga:searchUniques", "ga:transactionRevenue", "ga:transactions", "ga:revenuePerTransaction", "ga:goalValueAllPerSearch"),dimensions = c("ga:date", "ga:searchKeyword"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("banner_ecommerce_devicecategory", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c( "ga:searchUniques", "ga:transactionRevenue", "ga:transactions", "ga:revenuePerTransaction", "ga:goalValueAllPerSearch"),dimensions = c("ga:date", "ga:searchKeyword", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("campaign_sourcemedium", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:sessions", "ga:percentNewSessions", "ga:newUsers", "ga:bounceRate", "ga:pageviewsPerSession", "ga:avgSessionDuration", "ga:transactionsPerSession", "ga:transactions"),dimensions = c("ga:date", "ga:campaign", "ga:sourceMedium"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  #assign(paste0("internalSearch", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:sessions", "ga:percentNewSessions", "ga:newUsers", "ga:bounceRate", "ga:pageviewsPerSession", "ga:avgSessionDuration", "ga:transactionsPerSession", "ga:transactions"),dimensions = c("ga:date", "ga:searchKeyword"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  assign(paste0("sitecontent_allpages", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:pageviews", "ga:uniquePageviews", "ga:avgTimeOnPage", "ga:entrances", "ga:bounceRate", "ga:exitRate","ga:pageValue"),dimensions = c("ga:date", "ga:pagePath"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("sitecontent_allpages_device_category", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:pageviews", "ga:uniquePageviews", "ga:avgTimeOnPage", "ga:entrances", "ga:bounceRate", "ga:exitRate","ga:pageValue"),dimensions = c("ga:date", "ga:pagePath", "ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("top_events", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:totalEvents", "ga:uniqueEvents", "ga:eventValue", "ga:avgEventValue"),dimensions = c("ga:date","ga:eventCategory","ga:eventAction", "ga:eventLabel"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("top_eventsByDeviceCategory", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:totalEvents", "ga:uniqueEvents", "ga:eventValue", "ga:avgEventValue"),dimensions = c("ga:date","ga:eventCategory","ga:eventAction", "ga:eventLabel","ga:deviceCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  assign(paste0("color_pdp_desktop_mobile", substring(id, 4)),  get_ga(profileId = id, start.date = maxDate,end.date = "yesterday" , metrics = c("ga:totalEvents", "ga:uniqueEvents", "ga:eventValue", "ga:avgEventValue"),dimensions = c("ga:date","ga:eventAction", "ga:eventLabel", "ga:eventCategory"), sort = NULL, filters = NULL,segment = NULL, samplingLevel = NULL, start.index = NULL,max.results = NULL, include.empty.rows = NULL, fetch.by ="day", token), envir = .GlobalEnv)
  
  fileNames <<-c(fileNames, c(paste0("gaDataUser", substring(id, 4)) ,paste0("gaDataSession", substring(id, 4)), paste0("gaDataSources",  substring(id, 4)), paste0("gaDataEventTracking", substring(id, 4)), paste0("gaDataEcommerce", substring(id, 4)), paste0("gaDataEcommerce2", substring(id, 4)), paste0("gaDataEcommerce3", substring(id, 4)), paste0("gaDataEcommerce4", substring(id, 4)), paste0("gaDeviceData", substring(id, 4)),  paste0("usersAndSessionsGrouped", substring(id, 4)), paste0("pageViewsGroupedByPath", substring(id, 4)),  c(paste0("adwordsCost", substring(id, 4))), 
                              paste0("visitsDataBySourceMediumAndDevice", substring(id, 4)), paste0("appTracking", substring(id, 4)),
                              paste0("gaDataCampaignReportGroupDateCampaign", substring(id, 4)),
                              paste0("gaDataCampaignReport", substring(id, 4)),
                              paste0("dataSourcesWithRevenue", substring(id, 4)),
                              paste0("pageViewsGroupedByPathSourceMedium", substring(id, 4)),
                              paste0("landingpage", substring(id, 4)),
                              paste0("banner_ecommerce", substring(id, 4)),
                              paste0("campaign_sourcemedium", substring(id, 4)),
                              paste0("sitecontent_allpages", substring(id, 4)),
                              paste0("sitecontent_allpages_device_category", substring(id, 4)),
                              paste0("banner_ecommerce_devicecategory", substring(id, 4)),
                              paste0("top_events", substring(id, 4)),
                              paste0("campaignWithDevice", substring(id, 4)),
                              paste0("versioning_mobile", substring(id, 4)),
                              paste0("versioning_withoutScreenNamemobile", substring(id, 4)),
                              paste0("mobile_campaign", substring(id, 4)),
                              paste0("mobile_campaign_noTransactionId", substring(id, 4)),
                              paste0("hourlyScreenViews", substring(id, 4)),
                              paste0("UsersByHour", substring(id, 4)),
                              paste0("PDPByHour", substring(id, 4)),
                              paste0("impressions", substring(id, 4)),
                              paste0("pageBySourceMedium", substring(id, 4)),
                              paste0("fastDispatch", substring(id, 4)),
                              paste0("top_eventsByDeviceCategory", substring(id, 4)),
                              paste0("color_pdp_desktop_mobile", substring(id, 4))
  ));
}

tapply(viewIds, 1:length(viewIds),fetchData);

#print(testtest)

writeToHDFS <- function(fileName){
  print(fileName)
  print(get(fileName))
  hdfs.init()
  print("write hdfs")
  if(!is.null(get(fileName)) && !is.na(get(fileName))){
    if(file.exists(paste0("/home/rstudio/", fileName))){
      write.table(get(fileName), paste0("/home/rstudio/", fileName) , sep="," , append=TRUE, na = "NA", row.names=FALSE, col.names = FALSE)
    }else{
      write.table(get(fileName), paste0("/home/rstudio/", fileName) , sep="," , append=TRUE, na = "NA", row.names=FALSE, col.names = TRUE)
    }
    hdfs.put(paste0("/home/rstudio/", fileName), "/user/rstudio/")
  }
  #hdfs.close(modelfile)
  print("WRITIING")
}


tapply(fileNames, 1:length(fileNames),writeToHDFS)

write(yesterdayDate, "/home/rstudio/maxDateGAConsolidated", append = FALSE)

print("THE END")
print("#################################################################################")
@jdeboer
Copy link

jdeboer commented Jul 1, 2018

This appears to be a duplicate of #43

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants