Javascript Library

The Freshsales Javascript library helps you identify the people on your website as leads (or contacts), tracks their activities in real time, gives you a history of their past activities along with the pages they have viewed.

Getting Started

  1. Copy the snippet from Admin Settings > Integrations > Freshsales for Web > Javascript, which is prefilled with your "FRESHSALES_URL" and "FRESHSALES_APP_TOKEN".
  2. Or, replace "FRESHSALES_URL" and "FRESHSALES_APP_TOKEN" in the below snippet with your Freshsales portal’s URL and Application token and copy it.
    Copied Copy
    1
    2
    3
    <script> function createFcn(nm){(window.freshsales)[nm]=function(){(window.freshsales).push([nm].concat(Array.prototype.slice.call(arguments,0)))}; } (function(url,appToken,formCapture){window.freshsales=window.freshsales||[];if(window.freshsales.length==0){list='init identify trackPageView trackEvent set'.split(' ');for(var i=0;i<list.length;i++){var nm=list[i];createFcn(nm);}var t=document.createElement('script');t.async=1;t.src='//d952cmcgwqsjf.cloudfront.net/assets/analytics.js';var ft=document.getElementsByTagName('script')[0];ft.parentNode.insertBefore(t,ft);freshsales.init("FRESHSALES_URL","FRESHSALES_APP_TOKEN",true);}})();</script>
  3. Paste the library on the HTML header of your website.
  4. This script will automatically track all the forms of your website and push the form submissions as leads to Freshsales. In order to test this, please do a dummy form submission on your website and check "All leads" section in Freshsales under "Leads" tab, you should be able to see your Lead there.

    If certain data from your web form didn't get captured under lead properties, you can create custom fields in Freshsales with label same as that of the field on your website. This will solve the issue.

    If the above script works, you can skip the Create Leads and Update Leads/Contacts sections below. You are good to go!

    If the above script didn't work on your website i.e, didn't create lead from the form, this might be because of the way forms are loaded on your website. Loading forms in an iframe on your website using some third-party form providers can be one of the reasons for this. If that is the case, you can manually create leads in Freshsales by turning off the forms's auto tracking. In order to turn this off, you should replace true with false at the end of the above script. Now, you can have a look at Create Leads section to know more about creating Leads manually.

Create Leads

You can convert webform(signup, webinar registration form) submissions on your website to Leads in Freshsales. This can be done by following the below two steps:

  1. Pick the lead generation forms on your website.
  2. Call the identify method passing the user details, after the web form’s submission and validation. You can find more details about this method's payload in identify spec

identify method syntax:

1
2
3
freshsales.identify(userId, properties, successCallback, failureCallback);

identify method has the following fields:

FIELD TYPE DESCRIPTION
userID
(Required)
String This is the unique identification that you can give to your User. Usually Database ID will be used as userID. Refer userID section of identify spec to know more.
properties
(Optional)
Object This contains the information you know about the user. Refer properties section of identify spec to know more.
successCallback (Optional) Function A function that is executed after receiving the success response for identify call.
failureCallback (Optional) Function A function that is executed incase of failed identify calls.

Example identify call with hard-coded user information:

Copied Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
var sample_user_properties = { 'First name': 'John', 'Last name': 'Doe', 'Email': 'john.doe@example.com', 'company':{ 'Name': 'Example.com', 'Website': 'www.example.com' } }; freshsales.identify('john.doe@example.com', sample_user_properties);
EXPAND ↓

Note:
To capture website visitors as contacts instead of leads, use fs_contact in the above user payload.

You need to replace the hard-coded information in the above example call with the actual user information while calling identify. In case of native javascript, above identify call will look like:

Copied Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
var sample_user_properties = { 'First name': document.getElementById("first_name"), 'Last name': document.getElementById("last_name"), 'Email': document.getElementById("email"), 'company':{ 'Name': document.getElementById("company_name"), 'Website': document.getElementById("website") } }; freshsales.identify(document.getElementById("email"), sample_user_properties);
EXPAND ↓

Once you are done calling identify from your website, you can now see all the form submissions from your website getting created as leads in Freshsales.

Track Events

Track visitor activities on your website - like downloading a whitepaper, clicking on the social profiles’ follow buttons as events in Freshsales.

  1. Pick the specific call to action buttons that you’d like to be notified about.
  2. Call the trackEvent method on click of the button. You can find more details about this method's payload in trackEvent spec

trackEvent method syntax:

1
2
3
freshsales.trackEvent(eventName, eventProperties, successCallback, failureCallback);

trackEvent method has the following fields:

FIELD TYPE DESCRIPTION
eventName
(Required)
String The name of the event you are tracking. Refer eventName section of trackEvent spec to know more.
eventProperties
(Optional)
Object This contains the additional properties you know about the event. Refer eventProperties section of trackEvent spec to know more.
successCallback (Optional) Function A function that is executed after receiving the success response for trackEvent call.
failureCallback (Optional) Function A function that is executed incase of failed trackEvent calls.

Example trackEvent call with hard-coded event information:

Copied Copy
1
2
3
4
5
6
7
8
var sample_event_properties = { 'title': 'Subscribed Newsletter', 'email': 'john.doe@example.com' }; freshsales.trackEvent('Subscribed Newsletter', sample_event_properties);

Update Leads/Contacts

You can use the set method to update the existing leads based on their activity on your website.

set method syntax:

1
2
3
freshsales.set(properties, successCallback, failureCallback);

set method has the following fields:

FIELD TYPE DESCRIPTION
properties
(Optional)
Object This contains the information you know about the user. Refer properties section of identify spec to know more.
successCallback (Optional) Function A function that is executed after receiving the success response for set call.
failureCallback (Optional) Function A function that is executed incase of failed set calls.

Example set call with hard-coded user information:

Copied Copy
1
2
3
4
5
6
7
8
9
var sample_properties = { 'Address': '8691 MacGyver Mews', //Replace with address of user 'City': 'New Jermaine', //Replace with city of user 'Mobile': '13457879', //Replace with mobile number of user 'Alternate Contact Number': '98765432' //Replace with a custom field }; freshsales.set(sample_properties);

You need to replace the hard-coded information in the above example call with the actual user information while calling set. In case of native javascript, above set call will look like:

Copied Copy
1
2
3
4
5
6
7
8
9
10
var sample_user_properties = { 'Address': document.getElementById("address"), 'City': document.getElementById("city"), 'Mobile': document.getElementById("mobile"), 'Alternate Contact Number': document.getElementById("alt-cont-number") }; freshsales.set(sample_user_properties);
Help and Support
Send us a message
Chat with us