Ever wanted to do some analytics on tweets with a certain hashtag (#) or from a certain user (@)? Don’t look any further, there is a great tool called Microsoft Power BI. In this blog post, I’ll be explaining how to get twitter data into your Power BI. Let’s go!
First, we need to authorize ourselves on Twitter. This can be done by creating a Developer Account. Click on Sign Up in the upper right corner and create an account. Then go to apps.twitter.com and click Create New App. Now you need to fill in some details about your application. It does not really matter what you fill in for Name and Description, but your Name needs to be globally unique. For Website you can use your company website. Callback URL is not necessary for this project, so you don’t have to fill it in. Don’t forget to agree with the Twitter Developer Agreement at the end and simply confirm your application by clicking Create your Twitter application.
When you land on the application page, click the tab Keys and Access Tokens and then click on the bottom button Create my access token. Keep this page open because you will need the Consumer Key & Access Token later on.
Most work is finished now, so let’s load some data! Open Power BI and click Get Data and choose R Script. Yes, we will be using some R coding, but don’t worry! It’s very basic and my comments will explain everything.
You will see a window where you can input your R Script. Paste following code and click OK to confirm.
[sql] 1 #Check if all libraries are available. 2 list.of.packages <- c("twitteR","stringi") 3 new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] 4 if(length(new.packages)) install.packages(new.packages) 5 6 #Load Twitter Library 7 library(twitteR) 8 9 #Fill in these details with your personal consumer en access keys that you received on apps.twitter.com 10 consumer_key <- "xxxxx" 11 consumer_secret <- "xxxxx" 12 access_token <- "xxxxx" 13 access_secret <- "xxxxx" 14 15 #Initial authentication to twitter 16 setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret) 17 18 #What are we looking for on twitter? 19 search_term <- "#powerbi" 20 #How many tweets do you want to receive 21 number_of_tweets <- 1000 22 23 #Search on Twitter 24 tweets <- searchTwitter(search_term, number_of_tweets ) 25 26 #convert received data to something PowerBi understands 27 df_tweets <- twListToDF(tweets) 28 29 #column cleanup 30 df_tweets <- df_tweets[,c("text","favorited","favoriteCount","created","retweetCount","isRetweet","retweeted")] 31 df_tweets$text <- stri_encode(df_tweets$text, "", "UTF-8") [/sql]
First we will install the ‘twitteR’ library. Its a package created by Jeff Gentry. The package that he has developed will do all the hard work, so we don’t need to bother about it anymore. Second step is to fill in all the keys that we received from Twitter. After that the code will do some authorization with Twitter, so we are allowed to receive data. Now its our turn to tell the code what we are looking for and how many tweets we want to receive back. Next is the real search on Twitter and converting the received data in something readable for Power BI. Final part is cleaning up some columns and changing the code page of the tweets. Click OK to confirm the code. This might take a while depending on how many tweets you want to be returned. A navigator window will open. Select the data set df_tweets and click OK.
All the next steps are up to you! The data is now available in Power BI. Enjoy querying Twitter!
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |