Events Analytics

Identifying Visitors From Form Submissions

On a website not built on Liferay DXP, you can create a known individual in Analytics Cloud from the details a visitor submits in a form. The page must load the Analytics Cloud client and call Analytics.create(), as described in Tracking Events in Third Party Websites.

Sending the Submitted Fields

Pass the submitted values to Analytics.setIdentity() in its fields array. Each entry in the array is a field, an object with a name and a value. The client doesn’t send the name property that a basic setIdentity() call passes with email. To send the visitor’s name, use the firstName and lastName fields. When tracking is disabled, setIdentity() sends nothing.

Analytics Cloud stores the firstName, lastName, and country fields in the individual’s matching attributes, and the emailAddress field as the individual’s email address. When another data source, such as Liferay DXP, Salesforce, or Marketo, has a value for the same attribute, Analytics Cloud keeps that value instead of the submitted one.

Analytics Cloud creates a known individual only from a submission whose firstName, lastName, and emailAddress fields all have values. It doesn’t combine fields from separate submissions, so your form must collect all three and send them together. A submission from a suppressed user creates no known individual.

Passing the email address only in email identifies the visitor, but creates no known individual from the submission. The Analytics Cloud client hashes email in the browser, but sends fields values, including emailAddress, as entered.

This call passes the firstName, lastName, emailAddress, and country fields to setIdentity():

Analytics.setIdentity({
    fields: [
        { name: "firstName", value: "Replace with first name" },
        { name: "lastName", value: "Replace with last name" },
        { name: "emailAddress", value: "Replace with email address" },
        { name: "country", value: "Replace with country" },
    ],
});

Sending the Identity Before the Visitor Leaves

When the form sends the visitor to a page on a different origin (scheme, host, and port), such as a thank-you page on another domain, call Analytics.flush() after setIdentity(), and send the visitor to that page only after the Promise that flush() returns settles. Otherwise, the identity can stay queued on the form’s origin, and Analytics Cloud receives it only if the visitor later returns to a page on that origin that loads the client and calls Analytics.create(). When the next page is on the same origin and loads the client, that page sends the queued identity, and you don’t need Analytics.flush().

A settled Promise means the send attempt finished, not that Analytics Cloud accepted the identity. Each request times out after five seconds, but the client sends its queues one at a time, so the wait can take longer. The client also flushes on its own, every two seconds by default. If that flush is already running, flush() can return its Promise, which can settle before the identity is sent.

Example: Identifying Visitors From a Marketo Form

For a Marketo Forms 2.0 form on a page that loads the client and calls Analytics.create(), add this script in a <script> tag after the form’s Marketo embed code. The script calls setIdentity() with the submitted fields, calls Analytics.flush(), and sends the visitor to the form’s follow-up page after the Promise settles:

(function () {
    var FIELD_MAP = {
        Country: "country",
        Email: "emailAddress",
        FirstName: "firstName",
        LastName: "lastName"
    };

    var FORM_ID = null;

    if (typeof MktoForms2 === "undefined") {
        return;
    }

    function getFields(values) {
        return Object.keys(FIELD_MAP)
            .map(function (marketoName) {
                return {
                    name: FIELD_MAP[marketoName],
                    value: String(values[marketoName] || "").trim()
                };
            })
            .filter(function (field) {
                return field.value;
            });
    }

    MktoForms2.whenReady(function (form) {
        if (FORM_ID !== null && form.getId() !== FORM_ID) {
            return;
        }

        form.onSuccess(function (values, followUpUrl) {
            function go() {
                if (followUpUrl) {
                    location.href = followUpUrl;
                }
            }

            if (
                typeof Analytics === "undefined" ||
                typeof Analytics.setIdentity !== "function"
            ) {
                go();

                return false;
            }

            Analytics.setIdentity({
                fields: getFields(values)
            });

            if (typeof Analytics.flush !== "function") {
                go();

                return false;
            }

            Analytics.flush()
                .catch(function () {})
                .finally(go);

            return false;
        });
    });
})();

The script works like this:

  • FIELD_MAP maps each field name in your Marketo form to the field name Analytics Cloud reads. If your form uses different field names, change the keys to match them.
  • FORM_ID limits the script to one form. Set it to your form’s ID, which is the third argument to MktoForms2.loadForm() in your embed code. Write it as a number without quotes, such as 1234, even when the embed code quotes it. Leave it null to handle every Marketo form on the page.
  • Returning false from onSuccess stops Marketo’s own redirect. When the form has no follow-up page, the visitor stays on the page.
  • When the client isn’t ready, because it isn’t loaded or Analytics.create() hasn’t run, the script sends the visitor to the follow-up page without identifying them. When the client has no flush() method, the script calls setIdentity() but sends the visitor to the follow-up page without waiting.
  • When MktoForms2 is not on the page, the script does nothing.

Checking for the Known Individual

To confirm that a submission created a known individual, open Analytics Cloud, click Individuals in the left navigation panel, and click the Known Individuals tab. Analytics Cloud checks for new form submissions once an hour, so the visitor can take more than an hour to appear.

The Known Individuals tab lists each known individual by name and email address, with their job title, total activities, and last activity.