Skip to content

Instantly share code, notes, and snippets.

@byalextran
Last active May 18, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byalextran/57a7f8828f79923dcb2f73e56dc56ab6 to your computer and use it in GitHub Desktop.
Save byalextran/57a7f8828f79923dcb2f73e56dc56ab6 to your computer and use it in GitHub Desktop.
Ruby - Assign random numbers to HubSpot contacts
.env
vendor
.bundle

Assign Random Numbers to HubSpot Contacts

Some A/B tests work better when you can create lists of random contacts. This script helps facilitate that.

Instead of exporting a list of contacts, assigning random numbers, and importing, you can take advantage of the HubSpot API to do it all for you.

Installation

git clone https://gist.github.com/57a7f8828f79923dcb2f73e56dc56ab6.git hubspot-random-number
cd hubspot-random-number
gem install ruby-client
gem install dotenv

Configuration

# replace the placeholder key below with your HubSpot API key
echo "HAPIKEY = YOUR_HUBSPOT_API_KEY" > .env

Open hubspot-random-number.rb and update the variables at the top.

Usage

# this will process _all_ the contacts in your list. for large lists, be sure you
# keep an eye on API usage limits (10k in 24 hrs). divide your list size by
# 50 and that will be how many API calls this script makes.
ruby hubspot-random-number.rb
source "https://rubygems.org"
gem "json"
gem "rest-client"
gem "dotenv"
GEM
remote: https://rubygems.org/
specs:
domain_name (0.5.20160615)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.1.1)
http-cookie (1.0.2)
domain_name (~> 0.5)
json (2.0.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
netrc (0.11.0)
rest-client (2.0.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
PLATFORMS
ruby
DEPENDENCIES
dotenv
json
rest-client
BUNDLED WITH
1.11.2
##################################
# CHANGE THESE VARIABLES AS NEEDED
##################################
# the list id of the list with contacts you want to add a random number to
LIST_ID = 3395
# the internal name of the contact property you want storing the random number
INTERNAL_NAME = "lc_random_number"
# the random number will fall within this range
RANGE = 1..24
########################################
# DO NOT CHANGE ANYTHING BELOW THIS LINE
########################################
require "json"
require "rest-client"
require "dotenv"
Dotenv.load
BASE_API_URL = "https://api.hubapi.com"
COUNT = 100
def get_contacts(offset=0)
begin
response = RestClient.get "#{BASE_API_URL}/contacts/v1/lists/#{LIST_ID}/contacts/all?hapikey=#{ENV['HAPIKEY']}&count=#{COUNT}&vidOffset=#{offset}"
JSON.parse response
rescue => e
puts "Error getting contacts: #{e}"
exit
end
end
def update_contacts contacts
begin
RestClient.post "#{BASE_API_URL}/contacts/v1/contact/batch/?hapikey=#{ENV['HAPIKEY']}", contacts.to_json, :content_type => :json
rescue => e
puts "Error updating contacts: #{e}"
end
end
count = 0
offset = 0
loop do
puts "Updating contacts #{count*COUNT+1}-#{(count+1)*COUNT}..."
contacts = get_contacts offset
contacts_to_update = contacts["contacts"].map do |contact|
{ :vid => contact["vid"], :properties => [{ :property => INTERNAL_NAME, :value => "#{rand RANGE}" }] }
end
update_contacts contacts_to_update
break unless contacts["has-more"]
offset = contacts["vid-offset"]
count += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment