Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This library initializes beacon tagging and allows sending of tags.
- * Currently sending of reporting ID and data with the tags are not supported.
- * Creates the following global variables:
- * - window._bcq (required by the third party)
- * - window.beaconTaggingHelper (wrapper for this library)
- *
- * USAGE:
- * // Initialization (already handled by a script library in Signal)
- * var bd = beaconTaggingHelper.BEACON_PROD_DOMAIN;
- * var bh = beaconTaggingHelper.BEACON_TEST_DOMAIN;
- * beaconTaggingHelper.init(bd, bh); // Automatically run by Signal
- * // Sending a page load tag
- * var pageTag = beaconTaggingHelper.getCurrentPageTag();
- * if (!!pageTag) {
- * beaconTaggingHelper.tagAction(pageTag);
- * }
- * // Sending a click tag (just an example; should be handled by Signal tags and event bindings)
- * var button = document.querySelector("a.addToBagButton");
- * button.addEventListener("click", function() {
- * beaconTaggingHelper.tagAction("button:addToBag");
- * });
- */
- (function(global) {
- global._bcq = [];
- global.beaconTaggingHelper = new BeaconTaggingHelper();
- /**
- * Beacon Tagging helper library (constructor of window.beaconTaggingHelper)
- * @constructor
- */
- function BeaconTaggingHelper()
- {
- // Used for bd and bh parameters on initialization
- var BEACON_PROD_DOMAIN = "beacon.direct.asda.com";
- var BEACON_TEST_DOMAIN = "beacon.qa.direct.asda.com";
- // Mapping for tag actions and contexts
- var TAG_MAP = {
- "page:home" : { "action" : "HOMEPAGE_VIEW", "context" : "HomePage" },
- "page:search" : { "action" : "SEARCH_VIEW", "context" : "SearchResults" },
- "page:categoryListing" : { "action" : "BROWSE_VIEW", "context" : "Browse" },
- "page:categoryLanding" : { "action" : "CATEGORY_VIEW", "context" : "CategoryListings" },
- "page:productDetails" : { "action" : "PRODUCT_VIEW", "context" : "ProductPage" },
- "page:basket" : { "action" : "SHOPCART_VIEW", "context" : "ShoppingCart" },
- "action:addToBag" : { "action" : "ON_ATC", "context" : "CartHelper" },
- "action:saveForLater" : { "action" : "ON_LIST_CHANGE", "context" : "ShoppingCart" },
- "action:signIn" : { "action" : "ON_SIGNIN", "context" : "SignIn" }
- };
- var isDebug;
- var debugTitle = "[Signal Debug :: Beacon Tagging]";
- // "Publishing" of methods and "constants"
- return {
- BEACON_PROD_DOMAIN : BEACON_PROD_DOMAIN,
- BEACON_TEST_DOMAIN : BEACON_TEST_DOMAIN,
- init : init,
- getCurrentPageTag : getCurrentPageTag,
- tagAction : tagAction
- };
- /**
- * Initializes Beacon Tagging
- * @public
- * @param {string} bdParam Domain to use for beacon's "bd" parameter
- * @param {string} bhParam Domain to use for beacon's "bh" parameter
- * @returns {void}
- */
- function init(bdParam, bhParam, isDebugMode)
- {
- isDebug = !!isDebugMode;
- // Execute _setOptions command
- var command = "_setOptions";
- var params = {
- start_time: (new Date()).getTime(),
- bd: bdParam,
- bh: bhParam
- };
- _bcq.push([command, params]);
- // Request beacon.js (via script tag)
- var scriptSrc = "//" + bdParam + "/beacon.js?bd=" + bdParam + "&bh=" + bhParam;
- var scriptTag = document.createElement("script");
- scriptTag.setAttribute("type", "text/javascript");
- scriptTag.setAttribute("src", scriptSrc);
- scriptTag.setAttribute("async", "async");
- document.body.appendChild(scriptTag);
- isDebug && console.log(debugTitle, "_setOptions executed", params);
- }
- /**
- * Detects the type of the current page and returns the appropriate tag type
- * @public
- * @returns {string|null} The property name from TAG_MAP that corresponds to the current page type, or null
- */
- function getCurrentPageTag()
- {
- var pt = new PageTypeHelper(DATA_LAYER.store);
- var page;
- switch (true) {
- case pt.isBasketUrl() :
- page = "page:basket";
- break;
- case pt.isProductUrl() :
- page = "page:productDetails";
- break;
- case pt.isHomeUrl() :
- page = "page:home";
- break;
- case pt.isSearchUrl() :
- page = "page:search";
- break;
- case pt.isCategoryUrl() :
- page = (pt.isLandingUrl()) ? "page:categoryLanding" : "page:categoryListing";
- break;
- default :
- page = null;
- }
- return page;
- }
- /**
- * Sends a beacon tag (executes _tagAction command)
- * @public
- * @param {string} tag Should be a property in TAG_MAP (see above)
- * @returns {boolean} Whether _tagAction executed (false, if not a valid property name in TAG_MAP provided)
- */
- function tagAction(tag)
- {
- if (!(tag in TAG_MAP)) {
- isDebug && console.log(debugTitle, "_tagAction NOT executed (invalid tag name)", {tag: tag});
- return false;
- }
- var tagAttr = TAG_MAP[tag];
- _bcq.push(["_tagAction", tagAttr.context, tagAttr.action, "", []]);
- isDebug && console.log(debugTitle, "_tagAction executed",
- {tag: tag, context: tagAttr.context, action: tagAttr.action});
- return true;
- }
- }
- /**
- * Helper library for detecting page types. Create an instance for each page visit
- * @constructor
- * @param {object} store The react store (typically found in DATA_LAYER.store)
- */
- function PageTypeHelper(store)
- {
- var path = store && store.router && store.router.location && store.router.location.pathname || [];
- var params = getUrlParams(path && store.router.location.search || "");
- var header = store && store.header || {};
- var categoryId = getCategoryId(path);
- return {
- isBasketUrl : isBasketUrl,
- isProductUrl : isProductUrl,
- isHomeUrl : isHomeUrl,
- isSearchUrl : isSearchUrl,
- isPromoUrl : isPromoUrl,
- isCategoryUrl : isCategoryUrl,
- isLandingUrl : isLandingUrl
- };
- /**
- * Returns whether the page is basket
- * @public
- * @return {boolean}
- */
- function isBasketUrl()
- {
- return /^\/on\/demandware.store\/Sites-.+-Site\/.+\/Cart-Show.*$/.test(path);
- }
- /**
- * Returns whether the page is a product details one (including sets)
- * @public
- * @return {boolean}
- */
- function isProductUrl()
- {
- return /^.*\/.+,.+,pd\.html.*$/.test(path);
- }
- /**
- * Returns whether the page is home page
- * @public
- * @returns {boolean}
- */
- function isHomeUrl()
- {
- return (categoryId === "10" && !isSearchUrl(params) && !isPromoUrl(params));
- }
- /**
- * Returns whether the page is a search result one
- * @public
- * @returns {boolean}
- */
- function isSearchUrl()
- {
- return (!!params.q);
- }
- /**
- * Returns whether the page is a promotion listing one
- * @public
- * @returns {boolean}
- */
- function isPromoUrl()
- {
- return (!!params.pmid);
- }
- /**
- * Returns whether the page is a category one (either listing or landing)
- * @public
- * @returns {boolean}
- */
- function isCategoryUrl()
- {
- return !!categoryId;
- }
- /**
- * Returns whether the page is a landing category one (excludes listing)
- * @public
- * @returns {boolean}
- */
- function isLandingUrl()
- {
- // Check for non-category URL (false) or Home page (true)
- if (!categoryId) {
- return false;
- } else if (categoryId === "10") {
- return true;
- }
- // Check for landing according to store.header
- var isHeaderLanding;
- var landingCategories = header.landingCategories || null;
- if (landingCategories) {
- isHeaderLanding = (landingCategories.indexOf(categoryId) !== -1);
- } else {
- isHeaderLanding = header.navigation && header.navigation.isLandingCategory;
- }
- if (isHeaderLanding) {
- return true;
- }
- // Check for landing according to top navigation menu
- var menu = (header.navigation && header.navigation.menu || []);
- for (var i = 0, l = menu.length; i < l; i++) {
- if (menu[i] && menu[i].id === categoryId) {
- return true;
- }
- }
- return false;
- }
- /**
- * Converts query string to a parameter map
- * @private
- * @param {string} query Can optionally start with "?", can be empty
- * @returns {object} E.g. "?a=1&b=2" will yield result { a: 1, b: 2}
- */
- function getUrlParams(query)
- {
- var query = (query[0] !== "?") ? query : query.substr(1);
- query = query.split("&");
- var params = {};
- for (var i = 0, l = query.length; i < l; i++) {
- var param = query[i].split("=");
- params[param[0]] = (param.length > 1) ? param[1] : null;
- }
- return params;
- }
- /**
- * Returns the category ID of the page (or null for non-category pages)
- * @private
- * @param {string} path URL path, e.g. "/george/clothing/10,default,sc.html"
- * @returns {string|null} Either a category ID string or null
- */
- function getCategoryId(path)
- {
- var match = path.match(/^.*\/(.+),.+,sc\.html.*$/);
- return (match) ? match[1] : null;
- }
- }
- })(window);
- beaconTaggingHelper.init(
- beaconTaggingHelper.BEACON_PROD_DOMAIN,
- beaconTaggingHelper.BEACON_PROD_DOMAIN
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement