The Leadfeeder Tracker can automatically track changes in the page URLs to identify pageviews. This works in most of the 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 tracker.

Examples where the JavaScript API can be used:

  • Track transitions/virtual page views

  • Track non-page view events, such as in-page dialogs or video/media playback

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

API usage

The Leadfeeder tracker 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.

NOTE: When using the Leadfeeder JavaScript API it is good to keep in mind that Leadfeeder Tracker 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 tracker scripts on the web page, calling window.ldfdr.track() will send a tracking event for both.

pageview()

Sends a page view 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 Leadfeeder 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 Leadfeeder API's visits end-point.

identify()

Identify() is one of the methods that Leadfeeder’s JavaScript API exposes for the web page to call. Identify() allows you to use your existing ways of identifying website visitors within Leadfeeder. Please read more here.

How to know that it is safe to call window.ldfdr.getTracker() when using JS API:
You can inject following code into the script:

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

and if you want to use the lfClientId (visitor ID) then you can use:

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

Leadfeeder cookie expiration

As a default Leadfeeder cookie expires in two years (730 days). If wanted, you can use the following to make the cookie to expire after a shorter time:

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

If you use Leadfeeder’s Google Tag Manager template, another option is that you set up the expire time in Google Tag Manager settings as shown in the picture below:

NOTE: Please note 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 script in your GTM - check the template in GTM for possible updates.

Did this answer your question?