Skip to main content
All CollectionsLeadfeederTracking Script
Customize Your Site Tracking with JavaScript API
Customize Your Site Tracking with JavaScript API

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

Helena Hrubesova avatar
Written by Helena Hrubesova
Updated over a year ago

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

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

Please note: 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.

Please note: When using the Leadfeeder JavaScript API it is good to keep in mind that the 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 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 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 the 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 });

Dealfront cookie expiration

As a default the Dealfront cookie expires in two years (730 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 Dealfront's Google Tag Manager template, another option is to amend the 'Number of days until the Dealfront cookie expires' option. This is only applicable if you have the Dealfront cookie enabled.

Please note: When changing the cookie expiration time, this setting is taken into consideration only if you have enabled cookies in your Dealfront 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, feedback? Please let us know by contacting our support team via the chat or by sending us an email at support@dealfront.com.

RELATED:

Did this answer your question?