1.3.3 • Published 7 months ago

zenfi-sdk v1.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Zenfi JavaScript SDK

npm.io npm.io

JavaScript SDK for Zenfi partners. Useful to:

  • Auto-fill inputs with user data, making forms completion easier.
  • Track lead events (register, clicks, etc).

Usage

NodeJS

Install with npm or yarn:

npm install --save zenfi-sdk

Browser

Add the following script to your project:

<!-- Latest version -->
<script src="https://cdn.jsdelivr.net/gh/zenfi/js-sdk@main/dist/index.js" type="text/javascript"></script>

<!-- Or specify a version -->
<script src="https://cdn.jsdelivr.net/gh/zenfi/js-sdk@v1.3.3/dist/index.js" type="text/javascript"></script>

<!-- This will export a variable named "ZenfiSDK": -->
<script type="text/javascript">
  var zenfi = new ZenfiSDK(...args);
  zenfi.fillTargets();
</script>

API

new ZenfiSDK(...)

Creates a new instance of the Zenfi SDK.

Parameters

ParameterTypeDefault valueDescription
cookiesDomainString-The domain space where the cookies will be available. When this value is empty, cookies will be only accessible in the current domain / subdomain.
partnerNameString-The name of the platform which implements this code. This parameter is required to send events to the Zenfi API.
targetsObject[]The list of html elements and their configuration to be filled.
targets.dataKeyString-The name of the key in the leadInfo object which contains the information to be filled.
targets.selectorString | Function-The css selector to find the element in the DOM. If a function is passed, it will receive an object with { dataKey, strategy, value } as param properties and must return a string with the css selector.
targets.strategyEnum(value | text | click)valueIndicates the type of strategy to fill the DOM element. Possible values are: value: sets the value property of an input, text: puts the content inside the HTML tag as text, used for <div />s, click: triggers a click on the DOM element.
targets.beforeActionFunction-An optional function that is executed before the target is filled. It will receive an object with { selector, strategy, value } properties.
targets.afterActionFunction-An optional function that is executed after the target is filled. It will receive an object with { element, selector, strategy, value } properties (element is the DOM node being filled).

Example

const zenfi = new ZenfiSDK({
  partnerName: 'credifast',
  cookiesDomain: 'credifast.com',
  targets: [
    {
      dataKey: 'name',
      strategy: 'html',
      selector: '#nameInput',
    },
    {
      dataKey: 'email',
      strategy: 'value',
      selector: ({ dataKey }) => `#${dataKey}Input`,
      beforeAction: () => console.log('before filling info'),
      afterAction: ({ element }) => element.classList.add('changed'),
    },
  ],
});

.fetchData()

Fetches the lead information from the Zenfi API.

Parameters

This function takes no parameters.

Example

const zenfi = new ZenfiSDK();
await zenfi.fetchData();

console.log(zenfi.leadInfo);
/*
Prints:

{
  name: 'Chewbacca',
  email: 'chewy@lucasfilm.com'
}

*/

.fillTargets()

Auto-fills the labels and inputs specified in the targets configuration. It also fetches the lead information from the Zenfi API, if information is not available yet.

Parameters

This function takes no parameters.

Example

const zenfi = new ZenfiSDK({
  targets: [
    {
      dataKey: 'name',
      selector: '#nameInput',
      strategy: 'html',
    },
    {
      dataKey: 'email',
      strategy: 'value',
      selector: ({ dataKey }) => `#${dataKey}Input`,
      afterAction: ({ element }) => element.classList.add('changed'),
    },
  ]
});
await zenfi.fillTargets();
// It will fill the #nameInput and #emailInput nodes automatically.

.trackConversion(name, properties)

Sends a conversion event to the Zenfi API (the user is moving forward in the funnel): https://api.zenfi.mx/webhooks/leads/:partner/converted.

Parameters

ParameterTypeDescription
nameStringAny descriptive name for the event to be tracked (eg. register, click, addToCart).
propertiesObjectAny data related with the event to be tracked.

Example

zenfi.trackConversion('addToCart', {
  price: 100,
  amount: 2,
});
// It will track a "addToCart" event

.trackAbortion(name, properties)

Sends an abortion event to the Zenfi API (the user was rejected and can not move forward in the funnel): https://api.zenfi.mx/webhooks/leads/:partner/aborted.

Parameters

ParameterTypeDescription
nameStringAny descriptive name for the event to be tracked (eg. rejected, declinedOffer).
propertiesObjectAny data related with the event to be tracked.

Example

zenfi.trackConversion('declinedOffer', {
  reason: 'User got a better interest rate elsewhere'
});
// It will track a "declinedOffer" event

.trackPageView()

Sends an conversion event with name "PageView" to the Zenfi API. It includes metadata about the visited URL: href, hash, search, protocol, hostname and pathname.

Parameters

This function takes no parameters.

Example

zenfi.trackPageView();
// It will track a "PageView" event

.initPageViewsTracking()

Starts a listener that tracks page view events automatically.

Parameters

This function takes no parameters.

Example

zenfi.initPageViewsTracking();
// Listens to url changes and sends events

License

MIT

1.3.3

7 months ago

1.5.7

8 months ago

1.5.6

8 months ago

1.5.5

8 months ago

1.5.4

8 months ago

1.5.3

8 months ago

1.5.2

8 months ago

1.5.1

8 months ago

1.4.3

8 months ago