Segement is great, so too hubspot, however getting the two to work togeher… not so easy.
To start, the Cloud Mode using any number of the Segement Server side SDKs is easy, works like a charm.
Segments Device Mode integration to hubspot is a little more tricky.
a few items of note for the Segment & HubSpot integrationfirst hubspot fields are very sensitive, any spaces in fields that hubspot expects to not have spaces will result in a failure to tracksecond, the email field must be a well formated and accepted emailthird and IMPORTANT. the order in which and how the segment APIs are called will cause HubSpot to fail.1 - analytics.js must be called and a analyitcs.page called imideiatly after, the standard Segment Header script does this2- to correctly identify and track an action an analytics.identify and analyitcs.track events must be called after each other.// analytics.load()// analytics.page()// analytics.identify()// analytics.track()
thats the basics. its worth noting that the analytics.identify() command does most of the heavy lifting. The analytics.track() methods update the contact with associated events.
to test an integration i found Online Javascript Editor helpful and quick.
Note: clear your cache!! if not the analytics will link all your sessions and testing becomes a nightmare
<script>
//the analytics are wrapped in a function so that you can test the impact in the order and hubspot contact creation
function loadAnalytics(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement("script");n.type="text/javascript";n.async=!0;n.src="https://cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION="4.1.0";
analytics.load(“<your-segment-api-key);
analytics.page();
}}
function identifyUser(){
// the user ID isnt required. analytics.identify({
email: 'hstest123@hstest.com',
firstname: 'hstest',
lastname: '123'
})
}
function trackUser(){
analytics.track("Clicked Buy Now button", {
value: 20.5
})}
</script>
//each button executes hubspot in the required order if clicked from top to bottom
<input type=button value="loadAnalytics" onClick="loadAnalytics()"><br>
<input type=button value="identifyUser" onClick="identifyUser()"><br>
<input type=button value="trackUser" onClick="trackUser()"><br>
copy the code, add your segment api and give it a try, ensure that your source has hubspot set as the destination.
below is a fully working instrumented html page
<!DOCTYPE html>
<html lang="en">
<head>
<title> testing page- User Sign Up</title>
<script>
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement("script");n.type="text/javascript";n.async=!0;n.src="https://cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION="4.1.0";
analytics.load(<your-api-key>);
analytics.page();
}}();
</script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script>
//################################################
// a few items of note for the Segment & HubSpot integration
//first hubspot fields are very sensitive, any spaces in fields that hubspot expects to not have spaces will result in a failure to track
// second, the email field must be a well formated and accepted email
// third and IMPORTANT. the order in which and how the segment APIs are called will cause HubSpot to fail.
// 1 - analytics.js must be called and a analyitcs.page called imideiatly after, the standard Segment Header script does thsi
// 2- to correctly identify and track an action an analytics.identify and analyitcs.track events must be called after each other.
// analytics.load()
// analytics.page()
// analytics.identify()
// analytics.track()
//################################################
function beta_sign_up_page(){
analytics.page('segment event',{
section: 'service',
topic: 'topic ',
});
}
function identify_hubspot(){
var account_input = document.getElementById('account-Name').value;
var firstname_input = document.getElementById('first-Name').value;
var lastname_input = document.getElementById('last-Name').value;
var email_input = document.getElementById('email').value;
firstname_input = firstname_input.replace(/ /g, '');
lastname_input = lastname_input.replace(/ /g, '');
account_input = account_input.replace(/ /g, '');
analytics.identify({
email: email_input,
firstname: firstname_input,
lastname: lastname_input,
account_name: account_input,
})
}
function track_to_hubspot(){
var account_input = document.getElementById('account-Name').value;
var firstname_input = document.getElementById('first-Name').value;
var lastname_input = document.getElementById('last-Name').value;
var email_input = document.getElementById('email').value;
analytics.track('WZ Sign-Up Submitted - 1', {
email: email_input,
firstname: firstname_input,
lastname: lastname_input,
account_name: account_input,
});
}
function request_beta() {
// the order of these calls is important.
this.beta_sign_up_page();
this.identify_hubspot();
this.track_to_hubspot();
}
</script>
<body>
UUID Dsiplay: <span id="UUID_display">(click Log-IN)</span>
<p>sign up for beta</p>
account name : <input type="text" id="account-Name" value="account name">
<br/>
first name : <input type="text" id="first-Name" value="temp first1">
<br/>
last name : <input type="text" id="last-Name" value="temp last">
<br/>
<input type="text" id="email" value="test-testing@testing-segment.test">
<input type=button value="request beta" onClick="request_beta()"><br>
<br/>
</body>
</html>
happy integrating.