0

I'm using this script to download data from google trends. However,it doesn't print the last 3 days. In other words, I got results until 28/09/2020, and now it's 01/10/2020.

Is there a way to download even more recent data?

Thank you.

Note: the script is retrived from here.

 library(gtrendsR)
    library(tidyverse)
    library(lubridate)

get_daily_gtrend <- function(keyword = 'Taylor Swift', geo = 'UA', from = '2013-01-01', to = '2019-08-15') {
  if (ymd(to) >= floor_date(Sys.Date(), 'month')) {
    to <- floor_date(ymd(to), 'month') - days(1)
    
    if (to < from) {
      stop("Specifying \'to\' date in the current month is not allowed")
    }
  }

  mult_m <- gtrends(keyword = keyword, geo = geo, time = paste(from, to))$interest_over_time %>%
    group_by(month = floor_date(date, 'month')) %>%
    summarise(hits = sum(hits)) %>%
    mutate(ym = format(month, '%Y-%m'),
           mult = hits / max(hits)) %>%
    select(month, ym, mult) %>%
    as_tibble()
  
  pm <- tibble(s = seq(ymd(from), ymd(to), by = 'month'), 
               e = seq(ymd(from), ymd(to), by = 'month') + months(1) - days(1))
  
  raw_trends_m <- tibble()
  
  for (i in seq(1, nrow(pm), 1)) {
    curr <- gtrends(keyword, geo = geo, time = paste(pm$s[i], pm$e[i]))
    print(paste('for', pm$s[i], pm$e[i], 'retrieved', count(curr$interest_over_time), 'days of data'))
    raw_trends_m<- rbind(raw_trends_m,
                         curr$interest_over_time)
  }
  
  trend_m <- raw_trends_m %>%
    select(date, hits) %>%
    mutate(ym = format(date, '%Y-%m')) %>%
    as_tibble()
  
  trend_res <- trend_m %>%
    left_join(mult_m, by = 'ym') %>%
    mutate(est_hits = hits * mult) %>%
    select(date, est_hits) %>%
    as_tibble() %>%
    mutate(date = as.Date(date))
  
  return(trend_res)
}

get_daily_gtrend(keyword = 'Taylor Swift', geo = 'UA', from = '2013-01-01', to = '2019-08-15')
piravi
  • 109
  • 1
  • 6
  • 1
    Google trends aggregates data based on the period you're looking for. Range you're looking at it only gives you weekly level data I assume. You wouldn't really want a partial week of data. I think its for data <6 months that it gives you daily data. I should add that it's not advised to download weekly for say 5 years and then daily recently as Google indexes the data based on the time range you look at. – Jonny Phelps Oct 01 '20 at 12:30
  • What a pity. I really need even more recent data. do you think there is a way to solve this issue? – piravi Oct 01 '20 at 15:08
  • 1
    This looks promising: https://towardsdatascience.com/reconstruct-google-trends-daily-data-for-extended-period-75b6ca1d3420 – Jonny Phelps Oct 01 '20 at 15:37

1 Answers1

0

This is how Google Trends data works. Even if you go to the website and download data for anything beyond the last 7 days up to the last 90 days, it will give you daily data up to three days ago. So it is by design. I'm not certain whether gTrendsR retrieves hourly data, but you can either manually retrieve data for the last 7 days from the website to get hourly data right up to a few hours before your request, or use the PyTrends package, which can return hourly date. If you then have the hourly data, you can, of course, aggregate it easily to daily.