Skip to main content

Customize your site tracking with JavaScript API

Learn how you can automatically track changes in page URLs to identify pageviews in the Web Visitors app.

Written by Ruthie Keith

The Leadfeeder tracking script can automatically track changes in page URLs to identify pageviews in the Web Visitors app. This works in most cases, for both traditional web applications and single-page applications. However, if you wish to do more advanced tracking, the Leadfeeder JavaScript API can be useful to send custom events to the tracking script.

Examples where the JavaScript API can be used:

  • To track transitions/virtual pageviews

  • To track non-pageview events, such as in-page dialogs or video/media playback

If you use Google Tag Manager to install the Leadfeeder tracking script, remember to add <script> before the script and </script> to the end of the script.

API usage

The Leadfeeder tracking script exposes a set of methods for the web page to call if needed. All these methods exist under the window.ldfdr -object in the page.

When using the Leadfeeder JavaScript API it is good to keep in mind that the Leadfeeder tracking script supports multiple trackers on one page. The methods in the JavaScript API are designed so that, by default, the behavior is applied to all trackers. However, you can call them for specific tracker instances only. As an example, if you have two different Leadfeeder tracking scripts on the web page, calling window.ldfdr.track() will send a tracking event for both.

pageview()

Sends a pageview event. It supports arguments as an object.

window.ldfdr.pageview({ 
pageUrl: "https://www.example.com",
pageTitle: "Example page"
});

If the arguments are not present, it will default to window.location.href for pageUrl and document.title for pageTitle.

getAll()

Returns an array of all tracker instances on the page. See getTracker() below for details on how to interact with the tracker instances.

getTracker(trackerId)

Get a tracker instance by trackerId. For the tracker instance following methods are supported:

var tracker = window.ldfdr.getTracker("abcdef");
// Sends a pageview to specific tracker, supports
//same arguments as pageview() above
tracker.pageview();

getClientId()

You can obtain the clientId with the following call:

window.ldfdr.getTracker("abcdef").getClientId()

ClientID is the visitor ID that is stored in the Leadfeeder cookie. It matches to the lf_client_id field that is returned from the Web Visitors API's visits end-point.

identify()

Identify() is one of the methods that the Leadfeeder JavaScript API exposes for the web page to call. Identify() allows you to use your existing ways of identifying website visitors within Web Visitors. See How to track logged-in users on your site.
​

Check the tracking script is ready

How to know that it is safe to call window.ldfdr.getTracker() when using JS API:
​
Use this event listener to run code once the tracking script is ready:

document.addEventListener('ldfdr.trackerReady', function(event) { // do stuff });

To use the visitor ID (lfClientId), use:

document.addEventListener('ldfdr.trackerReady', function(event) { var id = event.tracker.getClientId(); // do stuff });

Leadfeeder cookie expiration

By default, the Leadfeeder cookie expires after one year (365 days). If preferred, you can use the following to make the cookie expire after a shorter time:

window.ldfdr('cfg','trackingCookieDurationDays',<numDays>,ss);

If you use Leadfeeder's Google Tag Manager template, another option is to amend the 'Number of days until the Leadfeeder cookie expires' option. This is only applicable if you have the Leadfeeder cookie enabled.

When changing the cookie expiration time, this setting is taken into consideration only if you have enabled cookies in your Leadfeeder cookie settings here. When it comes to using GTM, this requires that you are using the latest version of Leadfeeder tracking script in your GTM - check the template in GTM for possible updates.

--

Questions, comments or feedback? Contact our support team via chat or email us at support@leadfeeder.com.

Did this answer your question?