Skip to content

Instantly share code, notes, and snippets.

@stephlocke

stephlocke/hubspot.R

Last active Dec 21, 2018
Embed
What would you like to do?
Work with hubspot data
library(httr)
library(purrr)
library(dplyr)
library(tidyr)
apikey="demo"
get_deal_properties = function(apikey="demo"){
base_url = "https://api.hubapi.com"
properties_url = modify_url(base_url,
path ="/properties/v1/deals/properties/")
res = GET(properties_url, query=list(hapikey = apikey))
properties = map_chr(content(res), "name")
return(properties)
}
# get_deal_properties(apikey)
get_deals = function(apikey="demo",
properties=get_deal_properties(apikey),
property_history = "true",
associations = "true",
max_iter = 10){
base_url = "https://api.hubapi.com"
properties_url = modify_url(base_url, path ="/deals/v1/deal/paged")
deals = list()
n = 0
do = TRUE
offset = 0
while(do & n<max_iter){
res = GET(properties_url,
query=c(
list(
offset = offset,
hapikey = apikey,
limit = 250,
includeAssociations=associations,
propertiesWithHistory=property_history),
set_names(lapply(properties, function(x){x}),
rep("properties", length(properties)))
))
n = n + 1
deals[n] = list(content(res)$deals)
do = content(res)$hasMore
offset = content(res)$offset
}
deals = flatten(deals)
deals = set_names(deals, map_int(deals, "dealId"))
}
get_deal_associations = function(apikey = "demo",
deals = get_deals(apikey)){
deals %>%
map("associations") %>%
dplyr::data_frame(Ids = .) %>%
tidyr::unnest(.id = "deal") %>%
dplyr::mutate(deal = as.integer(deal))->
associations
associations %>%
dplyr::mutate(type = rep(c("contacts", "companies", "deals"),
nrow(associations)/3)) %>%
tidyr::unnest(Ids) %>%
tidyr::unnest(Ids)
}
#check = get_deal_associations(deals=get_deals(max_iter = 10))
get_deal_table = function(deals = get_deals(),
type = "basic"){
type = match.arg(type, c("basic", "properties", "properties with history"))
if(type == "basic"){
deals %>%
modify_depth(1,head, 3) %>%
map_df(as_data_frame) %>%
mutate_if(~sum(is.na(.))==sum(is.na(as.numeric(.))), as.numeric) ->
result
}
if(type == "properties"){
deals %>%
map("properties") %>%
modify_depth(2,~.$value) %>%
map_df(as_data_frame, .id = "dealId") %>%
mutate_if(~sum(is.na(.))==sum(is.na(as.numeric(.))), as.numeric) ->
result
}
if(type == "properties with history"){
deals %>%
map("properties") %>%
modify_depth(2,~.$`value`) %>%
map_df(as_data_frame,.id = "dealId") ->
result
# deals %>%
# map("properties") %>%
# modify_depth(2,~.$`versions`) %>%
# modify_depth(1,~as_data_frame(.)) %>%
# map_df(as_data_frame,.id = "dealId") %>%
# gather(field, value, -dealId) %>%
# mutate(name = map(value, "name"),
# timestamp = map(value, "timestamp"),
# sourceId = map(value, "sourceId"),
# source = map(value, "source"),
# sourceVid = map(value, "sourceVid"),
# value = map(value, "value"))
}
return(result)
}
#deals_tbl = get_deal_table(deals, type="properties")
get_deal_pipeline = function(deals = get_deals()){
deals %>%
map(c("properties","dealstage","versions")) %>%
flatten() %>%
map(head, 4) %>%
map_df(as_data_frame, .id="dealId")
}
#deal_stages = get_deal_pipeline(deals)
get_company_properties = function(apikey="demo"){
base_url = "https://api.hubapi.com"
properties_url = modify_url(base_url,
path ="/properties/v1/companies/properties/")
res = GET(properties_url, query=list(hapikey = apikey))
properties = map_chr(content(res), "name")
return(properties)
}
# get_company_properties(apikey)
get_companies = function(apikey="demo",
properties=get_company_properties(apikey),
property_history = "true",
max_iter = 10,
max_properties = 100){
base_url = "https://api.hubapi.com"
properties_url = modify_url(base_url, path ="/companies/v2/companies/paged")
properties = properties[1:max_properties]
companies = list()
n = 0
do = TRUE
offset = 0
while(do & n<max_iter){
res = GET(properties_url,
query=c(
list(
offset = offset,
hapikey = apikey,
limit = 250,
propertiesWithHistory=property_history),
set_names(lapply(properties, function(x){x}),
rep("properties", length(properties)))
))
n = n + 1
companies[n] = list(content(res)$companies)
do = content(res)$`has-more`
offset = content(res)$offset
}
companies = flatten(companies)
companies = set_names(companies, map_int(companies, "companyId"))
}
# companies = get_companies(apikey)
get_companies_table = function(companies = get_companies(),
type = "basic"){
type = match.arg(type, c("basic", "properties", "properties with history"))
if(type == "basic"){
companies %>%
modify_depth(1,head, 3) %>%
map_df(as_data_frame) %>%
mutate_if(~sum(is.na(.))==sum(is.na(as.numeric(.))), as.numeric) ->
result
}
if(type == "properties"){
companies %>%
map("properties") %>%
modify_depth(2,~.$value) %>%
map_df(as_data_frame, .id = "companyId") %>%
mutate_if(~sum(is.na(.))==sum(is.na(as.numeric(.))), as.numeric) ->
result
}
if(type == "properties with history"){
companies %>%
map("properties") %>%
modify_depth(2,~.$`value`) %>%
map_df(as_data_frame,.id = "companyId") ->
result
}
return(result)
}
# companies_tbl = get_companies_table(companies, type="properties")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment