Download your Tweet Activity report using R

My blog is now on Medium!

Recently I wanted to analyze my tweets' performance. As you may know, Twitter Analytics provides detailed data of a twitter account's tweets: number of impressions, engagement rate, number of retweets, link clicks, etc. What I didn't know was that the website restricts the maximum possible date range you can see (and download) to a 91-day window, but I wanted to analyze more data.



Instead of downloading reports 91 days at a time, I wrote an R script which does the dirty work. It downloads the tweet activity data from Twitter Analytics for any given period. If the selected date range is longer than 91 days, the download is split into several downloads.

Installation

To install the script you will need the devtoolspackage and the RSeleniumpackage:
install.packages("devtools")
library(devtools)
install_github("ropensci/RSelenium")
Then you can install the script directly from GitHub by running:
install_github("pferrando/TwitterAnalyticsDownloader")

How to use it

Once you have installed it, the first thing you need to do is loading it (just like with any other package). The main function is twitter_analytics_downloader, which will download one or more csv files containing tweets activity. See ?twitter_analytics_downloader for details.
You will have to provide:
  • A string specifying the phone, email or username of a Twitter account.
  • A string specifying the password of the Twitter account.
  • A string specifying the start date of the period to download in format "%Y-%m-%d".
  • A string specifying the end date of the period to download in format "%Y-%m-%d".
  • A string specifying the directory where the downloaded files have to be saved. Current directory is set by default.
You can download the Twitter Analytics report/s using the following code:
library(TwitterAnalyticsDownloader)
twitter_analytics_downloader("your-user-name", "your-password", "2018-01-01", "2018-04-01")

After running this code, you will see that a Chrome browser opens and starts the navigation to download the report/s. You just have to wait until the browser closes and you will have the files saved at the current directory. As simple as this!

You can find the full R code in my github account.

Hope it helps! Let me know your experience with the script leaving a comment or via twitter @pol_ferrando. 

If you found this post interesting, share it and subscribe to the blog!

Comments

Popular posts from this blog

Understanding how LIME explains predictions

Interpretable Machine Learning: An Overview

Understanding how to explain predictions with "explanation vectors"