Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save johndavidsimmons/fa4293380cfac438464c98667a3ec6eb to your computer and use it in GitHub Desktop.

Select an option

Save johndavidsimmons/fa4293380cfac438464c98667a3ec6eb to your computer and use it in GitHub Desktop.
// This variable grabs the query string from the anchors href element (the destination link)
// and makes formats it in order to pass it into URLSearchParams which makes a searchable object of
// the query parameters. Because the rule was triggered by an anchor element, "this" in the custom
// code refers to that anchor element
var qs = "?" + this.href.split("?")[1].split("#")[0]
// Creates an object of key/value pairs out of the query string
// this is the best way to grab the internal_link param
var params = new URLSearchParams(qs);
// Gets the internal link param
// The || syntax here says get the parameter or
// if that value is falsy, use "no promo number instead"
var promoNumber = params.get("internal_link") || "no promo number";
// Default common name as Cloudera did not provide one
var commonName = "Body Promo - ABM Page"
// If the data-attribute element exists, get it, else use fallback default values
var aemName = this.getAttribute("data-link-promo-AEM-name") || "no aem name";
var promoLevel = this.getAttribute("data-link-promo-level") || "no promo level";
// Setting copy variable to a default
// try to conditionally set it further down
var copy = "no copy";
// set the destination url as the href attribute
// set the button copy as the inner text of the element
// that contains the anchor
var href = this.href || "no destination url";
var buttonCopy = this.innerText || "no button copy";
// this is due to the headline copy elements being in different places
// parentElement is travering the DOM up to try and find the headline element
// headline elements change a lot, this is prone to break
// the "replace" at the end removes line breaks in the copy
try {
copy = this.parentElement.parentElement.querySelector("h4").innerText.replace("\n", "");
} catch (e) {}
s.linkTrackVars = "prop9,eVar78,eVar86,eVar125,eVar126,eVar127,events";
s.linkTrackEvents = "event125,event126";
s.eVar78 = _satellite.getVar('marketing:www:pagename');
s.eVar86 = _satellite.getVar('marketing:www:hostname') + _satellite.getVar('marketing:www:pathname');
s.prop9 = _satellite.getVar('marketing:www:hostname') + _satellite.getVar('marketing:www:pathname');
s.eVar125 = copy + " | " + promoNumber + " | " + commonName + " | " + aemName + " | " + promoLevel
s.eVar126 = buttonCopy + " | " + href;
// dynamically set event and send beacon
// event.$type contains the type of trigger that triggered the rule
var event = event.$type
if (event == "core.enters-viewport") {
s.events = "event125";
s.tl(this, "o", "Promo Component Impression");
} else if (event == "core.click") {
s.events = "event126";
s.useBeacon = true;
s.tl(this, "o", "Promo Component CTA");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment