if (!window.ubSnowplow || !window.ubSnowplow.initialized) { (function () { // Load snowplow script (this is provided by snowplow) (function (p, l, o, w, i, n, g) { if (!p[i]) { p.UnbounceSnowplowNamespace = p.UnbounceSnowplowNamespace || []; p.UnbounceSnowplowNamespace.push(i); p[i] = function () { (p[i].q = p[i].q || []).push(arguments); }; p[i].q = p[i].q || []; n = l.createElement(o); g = l.getElementsByTagName(o)[0]; n.async = 1; n.src = w; g.parentNode.insertBefore(n, g); } })( window, document, "script", "https://d34qb8suadcc4g.cloudfront.net/sp-2.14.0.js", "ubSnowplow" ); // Checks whether the current page is a "main" page var isMainPage = (function () { var usedAs = window.ub.page.usedAs; return function () { return usedAs === "main"; }; })(); // Gets an attribute from the "main" page var getAttrFromMainPage = function (attr) { try { return isMainPage() ? window.ub.page[attr] : window.parent.ub.page[attr]; } catch (e) { return undefined; } }; var getCookieValue = function (cookieName) { return document.cookie .split(";") .map(function (s) { return s.trim().split("="); }) .reduce(function (acc, v) { acc[v[0]] = v[1]; return acc; }, {})[cookieName]; }; // Function that performs all the main tracker configuration. var configureTracker = function () { // Page object used for the rest of this closure. var page = { id: getAttrFromMainPage("id"), currentId: window.ub.page.id, variantId: getAttrFromMainPage("variantId") }; var isConversionLink = function (element) { var href = (element && element.href) || ""; return href.indexOf("/clkg/") >= 0; }; // Creates custom contexts for events // CV-1760: routing strategy is hard coded for now until // we are able to read the value from the page var pageContext = function (type, eventMetadata) { return { schema: "js_tracker_context_v1.1.json", data: { pageId: page.id, variantId: page.variantId, eventType: type || "", eventMetadata: eventMetadata || [], routingStrategy: getCookieValue("ubrs") } }; }; // Don't do anything else if the page id could not be read or if a // subpage is being accessed directly if (!page.id || (!isMainPage() && page.id === page.currentId)) { return; } // Set up snowplow tracker window.ubSnowplow( "newTracker", "sp-ub", "events.ub-analytics.com", { appId: "landing_page", platform: "web", eventMethod: "get" } ); // Use visitor ID generated by PS2 window.ubSnowplow("setUserIdFromCookie", "ubvs"); // Only emit views on main pages if (isMainPage()) { window.ubSnowplow("trackPageView", null, [pageContext("visit")]); } // Track form submissions // We are using UB hooks as opposed to the `enableFormTracking` builtin // method so that we only emit events once we know the form has been // successfully submitted. window.ub.hooks.afterFormSubmit.push(function (event) { var eventMetadata = event.isConversionGoal ? ["conversion"] : []; var snowplowContext = { schema: "iglu:com.snowplowanalytics.snowplow/submit_form/jsonschema/1-0-0", // The `submit_form` schema requires the formId field to be present. // Even though we are not making any validations at this point, we are // setting this property to be compliant with the schema. // When testing the `enableFormTracking` the value gotten for // data was: {"formId":"FORM","formClasses":[],"elements":[]} data: { formId: "ub-form" } }; // TODO: Revisit this. // We are using the snowplow object in the parent frame to handle form // submission events emitted from within an iframe. Without this, // HTTP requests were canceled by the browser as the iframe where the // form submission occurs is often removed immediately. var sp = isMainPage() ? window.ubSnowplow : window.parent.ubSnowplow; sp("trackSelfDescribingEvent", snowplowContext, [ pageContext("form-submission", eventMetadata) ]); }); // Track conversion link clicks window.ubSnowplow( "enableLinkClickTracking", { filter: function (event) { return isConversionLink(event); } }, true, false, [pageContext("link-click", ["conversion"])] ); // Track non-conversion link clicks window.ubSnowplow( "enableLinkClickTracking", { filter: function (event) { return !isConversionLink(event); } }, true, false, [pageContext("link-click")] ); window.ubSnowplow.initialized = true; }; configureTracker(); })(); } else { console.log("Tracker already initialized."); }