Thin wrapper around gtrendsR::gtrends() that retries failed requests using
jittered exponential backoff. Google Trends regularly returns transient
errors (non-200 responses, HTTP 429 rate limits, 5xx server errors) and the
connection itself can fail intermittently; retrying with growing, randomized
waits is far more robust than failing on the first hiccup.
gtrends_with_backoff(
keyword = NA,
geo = "ch",
time = "today+5-y",
gprop = "web",
category = "0",
hl = "en-US",
low_search_volume = FALSE,
cookie_url = "http://trends.google.com/Cookies/NID",
tz = 0,
onlyInterest = FALSE,
retry = 6,
empty_retry = 4,
wait = 4,
max_wait = 30,
quiet = FALSE,
attempt = 1
)A character vector with the actual Google Trends query
keywords. Multiple keywords are possible using gtrends(c("NHL",
"NBA", "MLB", "MLS")).
A character vector denoting geographic regions for the query,
default to “all” for global queries. Multiple regions are possible
using gtrends("NHL", c("CA", "US")).
A string specifying the time span of the query. Possible values are:
Last hour
Last four hours
Last day
Last seven days
Past 30 days
Past 90 days
Past 12 months
Last five years (default)
Since the beginning of Google Trends (2004)
Time span between two dates (ex.: "2010-01-01 2010-04-03")
A character string defining the Google product for which the trend query if preformed. Valid options are:
"web" (default)
"news"
"images"
"froogle"
"youtube"
A character denoting the category, defaults to “0”.
A string specifying the ISO language code (ex.: “en-US” or “fr”). Default is “en-US”. Note that this is only influencing the data returned by related topics.
Logical. Should include low search volume regions?
A string specifying the URL from which to obtain cookies. Default should work in general; should only be changed by advanced users.
A number specifying the minutes the returned dates should be offset to UTC. Note the parameter 'time' above is specified in UTC. E.g. choosing "time=2018-01-01T01 2018-01-01T03" and "tz=-120" will yield data between 2018-01-01T03 and 2018-01-01T05, i.e. data specified to be in UTC+2.
If you only want the interest over time set it to TRUE.
Maximum number of attempts before giving up on a hard error (429, 5xx, network, ...).
Maximum attempts for a "no data returned" response before
accepting the window as genuinely empty (zero volume). Kept small: a truly
empty window never recovers, so retrying it the full retry ladder just
wastes time; a few quick attempts still let a transient rate limit (which
can also surface as "no data") recover. This is the lever that keeps a
rate-limited backfill from grinding for hours.
Base wait in seconds. The wait before attempt n grows
exponentially (wait * 2^(n - 1)), is capped at max_wait, and has random
jitter added to avoid synchronized retries hammering the server.
Upper bound (seconds) for a single backoff wait.
If TRUE, suppress progress messages.
Internal counter, do not set manually.