Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This library initializes beacon tagging and allows sending of tags.
  3.  * Currently sending of reporting ID and data with the tags are not supported.
  4.  * Creates the following global variables:
  5.  *     - window._bcq (required by the third party)
  6.  *     - window.beaconTaggingHelper (wrapper for this library)
  7.  *
  8.  * USAGE:
  9.  * // Initialization (already handled by a script library in Signal)
  10.  * var bd = beaconTaggingHelper.BEACON_PROD_DOMAIN;
  11.  * var bh = beaconTaggingHelper.BEACON_TEST_DOMAIN;
  12.  * beaconTaggingHelper.init(bd, bh); // Automatically run by Signal
  13.  * // Sending a page load tag
  14.  * var pageTag = beaconTaggingHelper.getCurrentPageTag();
  15.  * if (!!pageTag) {
  16.  *   beaconTaggingHelper.tagAction(pageTag);
  17.  * }
  18.  * // Sending a click tag (just an example; should be handled by Signal tags and event bindings)
  19.  * var button = document.querySelector("a.addToBagButton");
  20.  * button.addEventListener("click", function() {
  21.  *   beaconTaggingHelper.tagAction("button:addToBag");
  22.  * });
  23.  */
  24. (function(global) {
  25.    
  26.     global._bcq = [];
  27.     global.beaconTaggingHelper = new BeaconTaggingHelper();
  28.    
  29.     /**
  30.      * Beacon Tagging helper library (constructor of window.beaconTaggingHelper)
  31.      * @constructor
  32.      */
  33.     function BeaconTaggingHelper()
  34.     {
  35.         // Used for bd and bh parameters on initialization
  36.         var BEACON_PROD_DOMAIN = "beacon.direct.asda.com";
  37.         var BEACON_TEST_DOMAIN = "beacon.qa.direct.asda.com";
  38.         // Mapping for tag actions and contexts
  39.         var TAG_MAP = {
  40.             "page:home" : { "action" : "HOMEPAGE_VIEW", "context" : "HomePage" },
  41.             "page:search" : { "action" : "SEARCH_VIEW", "context" : "SearchResults" },
  42.             "page:categoryListing" : { "action" : "BROWSE_VIEW", "context" : "Browse" },
  43.             "page:categoryLanding" : { "action" : "CATEGORY_VIEW", "context" : "CategoryListings" },
  44.             "page:productDetails" : { "action" : "PRODUCT_VIEW", "context" : "ProductPage" },
  45.             "page:basket" : { "action" : "SHOPCART_VIEW", "context" : "ShoppingCart" },
  46.             "action:addToBag" : { "action" : "ON_ATC", "context" : "CartHelper" },
  47.             "action:saveForLater" : { "action" : "ON_LIST_CHANGE", "context" : "ShoppingCart" },
  48.             "action:signIn" : { "action" : "ON_SIGNIN", "context" : "SignIn" }
  49.         };
  50.         var isDebug;
  51.         var debugTitle = "[Signal Debug :: Beacon Tagging]";
  52.        
  53.         // "Publishing" of methods and "constants"
  54.         return {
  55.             BEACON_PROD_DOMAIN : BEACON_PROD_DOMAIN,
  56.             BEACON_TEST_DOMAIN : BEACON_TEST_DOMAIN,
  57.             init : init,
  58.             getCurrentPageTag : getCurrentPageTag,
  59.             tagAction : tagAction
  60.         };
  61.        
  62.         /**
  63.          * Initializes Beacon Tagging
  64.          * @public
  65.          * @param {string} bdParam  Domain to use for beacon's "bd" parameter
  66.          * @param {string} bhParam  Domain to use for beacon's "bh" parameter
  67.          * @returns {void}
  68.          */
  69.         function init(bdParam, bhParam, isDebugMode)
  70.         {
  71.             isDebug = !!isDebugMode;
  72.             // Execute _setOptions command
  73.             var command = "_setOptions";
  74.             var params = {
  75.                 start_time: (new Date()).getTime(),
  76.                 bd: bdParam,
  77.                 bh: bhParam
  78.             };
  79.             _bcq.push([command, params]);
  80.             // Request beacon.js (via script tag)
  81.             var scriptSrc = "//" + bdParam + "/beacon.js?bd=" + bdParam + "&bh=" + bhParam;
  82.             var scriptTag = document.createElement("script");
  83.             scriptTag.setAttribute("type", "text/javascript");
  84.             scriptTag.setAttribute("src", scriptSrc);
  85.             scriptTag.setAttribute("async", "async");
  86.             document.body.appendChild(scriptTag);
  87.             isDebug && console.log(debugTitle, "_setOptions executed", params);
  88.         }
  89.    
  90.         /**
  91.          * Detects the type of the current page and returns the appropriate tag type
  92.          * @public
  93.          * @returns {string|null}  The property name from TAG_MAP that corresponds to the current page type, or null
  94.          */
  95.         function getCurrentPageTag()
  96.         {
  97.             var pt = new PageTypeHelper(DATA_LAYER.store);
  98.             var page;
  99.             switch (true) {
  100.                 case pt.isBasketUrl() :
  101.                     page = "page:basket";
  102.                     break;
  103.                 case pt.isProductUrl() :
  104.                     page = "page:productDetails";
  105.                     break;
  106.                 case pt.isHomeUrl() :
  107.                     page = "page:home";
  108.                     break;
  109.                 case pt.isSearchUrl() :
  110.                     page = "page:search";
  111.                     break;
  112.                 case pt.isCategoryUrl() :
  113.                     page = (pt.isLandingUrl()) ? "page:categoryLanding" : "page:categoryListing";
  114.                     break;
  115.                 default :
  116.                     page = null;
  117.             }
  118.             return page;
  119.         }
  120.        
  121.         /**
  122.          * Sends a beacon tag (executes _tagAction command)
  123.          * @public
  124.          * @param {string} tag  Should be a property in TAG_MAP (see above)
  125.          * @returns {boolean}  Whether _tagAction executed (false, if not a valid property name in TAG_MAP provided)
  126.          */
  127.         function tagAction(tag)
  128.         {
  129.             if (!(tag in TAG_MAP)) {
  130.                 isDebug && console.log(debugTitle, "_tagAction NOT executed (invalid tag name)", {tag: tag});
  131.                 return false;
  132.             }
  133.             var tagAttr = TAG_MAP[tag];
  134.             _bcq.push(["_tagAction", tagAttr.context, tagAttr.action, "", []]);
  135.             isDebug && console.log(debugTitle, "_tagAction executed",
  136.                 {tag: tag, context: tagAttr.context, action: tagAttr.action});
  137.             return true;
  138.         }
  139.        
  140.     }
  141.    
  142.     /**
  143.      * Helper library for detecting page types. Create an instance for each page visit
  144.      * @constructor
  145.      * @param {object} store  The react store (typically found in DATA_LAYER.store)
  146.      */
  147.     function PageTypeHelper(store)
  148.     {
  149.         var path = store && store.router && store.router.location && store.router.location.pathname || [];
  150.         var params = getUrlParams(path && store.router.location.search || "");
  151.         var header = store && store.header || {};
  152.         var categoryId = getCategoryId(path);
  153.        
  154.         return {
  155.             isBasketUrl : isBasketUrl,
  156.             isProductUrl : isProductUrl,
  157.             isHomeUrl : isHomeUrl,
  158.             isSearchUrl : isSearchUrl,
  159.             isPromoUrl : isPromoUrl,
  160.             isCategoryUrl : isCategoryUrl,
  161.             isLandingUrl : isLandingUrl
  162.         };
  163.        
  164.         /**
  165.          * Returns whether the page is basket
  166.          * @public
  167.          * @return {boolean}
  168.          */
  169.         function isBasketUrl()
  170.         {
  171.             return /^\/on\/demandware.store\/Sites-.+-Site\/.+\/Cart-Show.*$/.test(path);
  172.         }
  173.  
  174.         /**
  175.          * Returns whether the page is a product details one (including sets)
  176.          * @public
  177.          * @return {boolean}
  178.          */
  179.         function isProductUrl()
  180.         {
  181.             return /^.*\/.+,.+,pd\.html.*$/.test(path);
  182.         }
  183.        
  184.         /**
  185.          * Returns whether the page is home page
  186.          * @public
  187.          * @returns {boolean}
  188.          */
  189.         function isHomeUrl()
  190.         {
  191.             return (categoryId === "10" && !isSearchUrl(params) && !isPromoUrl(params));
  192.         }
  193.        
  194.         /**
  195.          * Returns whether the page is a search result one
  196.          * @public
  197.          * @returns {boolean}
  198.          */
  199.         function isSearchUrl()
  200.         {
  201.             return (!!params.q);
  202.         }
  203.        
  204.         /**
  205.          * Returns whether the page is a promotion listing one
  206.          * @public
  207.          * @returns {boolean}
  208.          */
  209.         function isPromoUrl()
  210.         {
  211.             return (!!params.pmid);
  212.         }
  213.        
  214.         /**
  215.          * Returns whether the page is a category one (either listing or landing)
  216.          * @public
  217.          * @returns {boolean}
  218.          */
  219.         function isCategoryUrl()
  220.         {
  221.             return !!categoryId;
  222.         }
  223.        
  224.         /**
  225.          * Returns whether the page is a landing category one (excludes listing)
  226.          * @public
  227.          * @returns {boolean}
  228.          */
  229.         function isLandingUrl()
  230.         {
  231.             // Check for non-category URL (false) or Home page (true)
  232.             if (!categoryId) {
  233.                 return false;
  234.             } else if (categoryId === "10") {
  235.                 return true;
  236.             }
  237.             // Check for landing according to store.header
  238.             var isHeaderLanding;
  239.             var landingCategories = header.landingCategories || null;
  240.             if (landingCategories) {
  241.                 isHeaderLanding = (landingCategories.indexOf(categoryId) !== -1);
  242.             } else {
  243.                 isHeaderLanding = header.navigation && header.navigation.isLandingCategory;
  244.             }
  245.             if (isHeaderLanding) {
  246.                 return true;
  247.             }
  248.             // Check for landing according to top navigation menu
  249.             var menu = (header.navigation && header.navigation.menu || []);
  250.             for (var i = 0, l = menu.length; i < l; i++) {
  251.                 if (menu[i] && menu[i].id === categoryId) {
  252.                     return true;
  253.                 }
  254.             }
  255.             return false;
  256.         }
  257.        
  258.         /**
  259.          * Converts query string to a parameter map
  260.          * @private
  261.          * @param {string} query  Can optionally start with "?", can be empty
  262.          * @returns {object}  E.g. "?a=1&b=2" will yield result { a: 1, b: 2}
  263.          */
  264.         function getUrlParams(query)
  265.         {
  266.             var query = (query[0] !== "?") ? query : query.substr(1);
  267.             query = query.split("&");
  268.             var params = {};
  269.             for (var i = 0, l = query.length; i < l; i++) {
  270.                 var param = query[i].split("=");
  271.                 params[param[0]] = (param.length > 1) ? param[1] : null;
  272.             }
  273.             return params;
  274.         }
  275.        
  276.         /**
  277.          * Returns the category ID of the page (or null for non-category pages)
  278.          * @private
  279.          * @param {string} path  URL path, e.g. "/george/clothing/10,default,sc.html"
  280.          * @returns {string|null} Either a category ID string or null
  281.          */
  282.         function getCategoryId(path)
  283.         {
  284.             var match = path.match(/^.*\/(.+),.+,sc\.html.*$/);
  285.             return (match) ? match[1] : null;
  286.         }
  287.     }
  288.        
  289. })(window);
  290.  
  291. beaconTaggingHelper.init(
  292.     beaconTaggingHelper.BEACON_PROD_DOMAIN,
  293.     beaconTaggingHelper.BEACON_PROD_DOMAIN
  294. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement