Skip to content

Instantly share code, notes, and snippets.

@luigi luigi/counter.rb
Last active Oct 27, 2015

Embed
What would you like to do?
Calculate the percentage of tweets linking to a website that came from the Tweet button
#
# Before running:
# $ gem install twitter
#
# Register a Twitter application to get auth credentials:
# https://dev.twitter.com/apps
#
# To run:
# $ ruby counter.rb upworthy.com 500
#
# Where 'upworthy.com' is the website and '500' is the number of tweets to analyze.
#
# Context:
# http://luigimontanez.com/2012/actually-social-media-buttons-work-really-well
#
require 'rubygems'
require 'twitter'
search_term = ARGV[0]
num_tweets = ARGV.fetch(1, 100).to_i
total_tweets = 0
from_button = 0
from_ios = 0
from_android = 0
from_web = 0
from_facebook = 0
from_hootsuite = 0
from_ifttt = 0
from_twitterfeed = 0
from_dlvr = 0
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"
end
options = {:count => num_tweets, :result_type => 'recent'}
client.search(search_term, result_type: "recent").take(num_tweets).each do |tweet|
total_tweets += 1
from_button += 1 if tweet.source =~ /Twitter for Websites/
from_ios += 1 if tweet.source =~ /iOS/
from_ios += 1 if tweet.source =~ /iPhone/
from_ios += 1 if tweet.source =~ /iPad/
from_android += 1 if tweet.source =~ /Android/
from_web += 1 if tweet.source =~ /Web Client/
from_facebook += 1 if tweet.source =~ /Facebook/
from_hootsuite += 1 if tweet.source =~ /Hootsuite/
from_ifttt += 1 if tweet.source =~ /IFTTT/
from_twitterfeed += 1 if tweet.source =~ /twitterfeed/
from_dlvr += 1 if tweet.source =~ /dlvr/
max_id = tweet.id
end
puts "Search term: #{search_term}"
puts "Total tweets analyzed: #{total_tweets}"
puts "Tweets from button: #{from_button} (#{sprintf('%.1f', (from_button.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from iOS: #{from_ios} (#{sprintf('%.1f', (from_ios.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from Android: #{from_android} (#{sprintf('%.1f', (from_android.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from Twitter.com: #{from_web} (#{sprintf('%.1f', (from_web.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from Facebook: #{from_facebook} (#{sprintf('%.1f', (from_facebook.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from Hootsuite: #{from_hootsuite} (#{sprintf('%.1f', (from_hootsuite.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from IFTTT: #{from_ifttt} (#{sprintf('%.1f', (from_ifttt.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from twitterfeed: #{from_twitterfeed} (#{sprintf('%.1f', (from_twitterfeed.to_f / total_tweets.to_f) * 100)}%)"
puts "Tweets from dlvr.it: #{from_dlvr} (#{sprintf('%.1f', (from_dlvr.to_f / total_tweets.to_f) * 100)}%)"
@glassresistor

This comment has been minimized.

Copy link

glassresistor commented Oct 16, 2013

/var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/request/multipart.rb:4: warning: already initialized constant DEFAULT_BOUNDARY
/var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/response/raise_error.rb:21:in on_complete': Unable to verify your credentials (Twitter::Error::Forbidden) from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/response.rb:9:incall'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/response.rb:63:in on_complete' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/response.rb:8:incall'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/request/url_encoded.rb:14:in call' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/request/multipart.rb:13:incall'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/request/multipart_with_file.rb:14:in call' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:253:inrun_request'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:118:in post' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:108:insend'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:108:in request' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:72:inpost'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/api/utils.rb:82:in send' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/api/utils.rb:82:inobject_from_response'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/api/oauth.rb:24:in token' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:96:inrequest_setup'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:108:in get' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:249:inrun_request'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:264:in build_request' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/request.rb:35:increate'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/request.rb:34:in tap' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/request.rb:34:increate'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:260:in build_request' from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:245:inrun_request'
from /var/lib/gems/1.8/gems/faraday-0.8.8/lib/faraday/connection.rb:106:in get' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:108:insend'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:108:in request' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/client.rb:66:inget'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/api/utils.rb:82:in send' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/api/utils.rb:82:inobject_from_response'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter/api/search.rb:32:in search' from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter.rb:60:insend'
from /var/lib/gems/1.8/gems/twitter-4.8.1/lib/twitter.rb:60:in method_missing' from counter.rb:21 from counter.rb:19:ineach'
from counter.rb:19

@luigi

This comment has been minimized.

Copy link
Owner Author

luigi commented Nov 5, 2013

@glassresistor I just updated the script with some fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.