ee-pltv

2.0.4 • Public • Published

Enrollment Engine Predictive Lifetime Value

A libary for caculating a users ltv based on userAgent, geo and course.

Installation

npm install

Usage

You will need to set a window variable called _eePltvConfigV2. The variable must be a javascript object and has only one key to set which is onLoad. This is a callback that will be executed as soon as the library is loaded. The onLoad will return an API object with the following methods:

  • uaToDeviceType: takes a user agent string as an argument and returns the device type based on https://github.com/faisalman/ua-parser-js.
  • uaToOSName: takes a user agent string as an argument and returns the OS name based on https://github.com/faisalman/ua-parser-js.
  • initSentry: takes the Sentry Dsn string key as an argument and initializes Sentry on the landing page, which will capture when our EE server fails to respond.
  • calculatePltv: takes an URL string and a user agent string as the arguments and returns an object containing three properties: pltv, default_value and debug_mode. The value of pltv is the predictive lifetime value. default_value is a boolean indicating whether pltv value is the calculated by the ML endpoint or a default one. debug_mode is a boolean configured on a program level and its purpose is to determine whether the fb purchase event should be sent or not according to the pltv correctness. For Halfsies project you also need to pass an extra param entryToken when calling the method. You will get this token on the queryparams of URL after the Native Form POST response.

See below for a typical usage for Halfsies Project. The use of the keywords async and await are recommended here because the LandingPageTemplate js is compiled on the EE so they are supported. In case you also want to use these where the js is not compiled you need to use Promises instead, so check the example for the Marketing Thank You pages.

/* On LandingPageTemplate.js*/
import { API } from 'ee-pltv'

afterSubmit: async () => {

  window._eePltvConfigV2 = {
    ...window._eePltvConfigV2,
  }
  try {
    API.initSentry(sentryDsn);
    const pltvData = await API.calculatePltv({
      urlString: window.location.href,
      uaString: window.navigator.userAgent,
      entryToken,
    });
    console.log('this is the response from PLTV ', pltvData.pltv);
  } catch (error) {
    console.log('an error occurred', error);
  } finally {
    /*
      If there is a redirect path set then use it. If not then reload the page.
    */
  }
}


/* If you want to test it pointint to staging, then on LandingPageTemplate.html.erb */
<% if ENV['PLTV_STAGING_URL'] %>
  <script>
    window._eePltvConfigV2 = {};
    window._eePltvConfigV2.PLTV_URL = "<%= ENV['PLTV_STAGING_URL'] %>"
  </script>
<% end %>

Now see below for a typical usage on the Marketing Thank You pages

window._eePltvConfigV2 = {
  onLoad: function (API) {
    API.initSentry(sentryDsn);
    API.calculatePltv({ urlString: window.location.href, uaString: window.navigator.userAgent })
      .then(function (pltvData) {
        console.log('this is the response from PLTV ', pltvData.pltv);
      }).catch(function (error) {
        console.log('an error occurred', error);
      })
  },
};

var ee_head = document.getElementsByTagName('head')[0];
var ee_script = document.createElement('script');
ee_script.type = 'text/javascript';
ee_script.async = true;
ee_script.src = 'https://<link-to-version-on-CDN>';
ee_head.appendChild(ee_script);

Start Dev Server

npm start

This command opens a HTML file with the development script.

Build Prod Version

npm run build

Testing production version

After builiding the script with the npm run build command, the file dist/ee-pltv.js will be accessible.

To run the production script in a local HTML file run open ./playground/dist-index.html

Features:

Readme

Keywords

Package Sidebar

Install

npm i ee-pltv

Weekly Downloads

105

Version

2.0.4

License

MIT

Unpacked Size

166 kB

Total Files

8

Last publish

Collaborators

  • jessezepedaocon