Search code examples
rgmailr

gmailr package Query Gmail Stats with R


I am trying to pull stats from the gmail API using R. I am using gmailr package. I am able to send an email from R but that's about where it ends. I was using these links to try to figure this out but it does not seem to be working: https://github.com/jennybc/send-email-with-r/#prep-work-related-to-gmail-and-the-gmailr-package and https://github.com/alkashef/gmailstats

All I really want to see is how many daily emails are received, and if possible, break it down by the hour.

Any assistance in this would be wonderful.

Thanks


Solution

  • You could try

    library(gmailr)
    clientid <- "...."
    key <- "...."
    token <- gmail_auth("read_only", clientid, key)
    msgs <- messages(search = "before:2018-01-01 after:2016-01-01", include_spam_trash = FALSE)
    msgs_meta <- lapply(id(msgs), message, format = "metadata")
    dates <- lubridate::dmy_hms(sapply(msgs_meta, date))
    addmargins(table(as.Date(dates), factor(format(dates, "%H"), levels = 0:23)))
    #             0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Sum
    # 2016-11-10  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0   1
    # 2016-11-11  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0   1
    # ...
    # 2017-12-27  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0   1
    # Sum         0  0  0  0  0  0  0  0  0  0 15  5  1  0  0  4  3  2  2  0  1 22 19  3  77
    

    The explanations are pretty much found in the links that you provided yourself.