src/anchor-click-interceptor.js
/**
 * Handle click on links and replace variables in the click URL.
 * The function changes the actual href value and stores the
 * template in the ORIGINAL_HREF_ATTRIBUTE attribute
 * @param {!./service/ampdoc-impl.AmpDoc} ampdoc
 * @param {!Event} e
 */
Handle click on links and replace variables in the click URL.
The function changes the actual href value and stores the
template in the ORIGINAL_HREF_ATTRIBUTE attribute
@param {!./service/ampdoc-impl.AmpDoc} ampdoc
@param {!Event} e
Handle click on links and replace variables in the click URL . The function changes the actual href value and stores the template in the ORIGINAL_HREF_ATTRIBUTE attribute @param { ! . / service / ampdoc - impl . AmpDoc } ampdoc @param { !Event } e
function maybeExpandUrlParams(ampdoc, e) {
  const target = closestByTag(dev().assertElement(e.target), 'A');
  if (!target || !target.href) {
    return;
  }
  const hrefToExpand =
      target.getAttribute(ORIG_HREF_ATTRIBUTE) || target.getAttribute('href');
  if (!hrefToExpand) {
    return;
  }
  const vars = {
    'CLICK_X': () => {
      return e.pageX;
    },
    'CLICK_Y': () => {
      return e.pageY;
    },
  };
  const newHref = Services.urlReplacementsForDoc(ampdoc).expandSync(
      hrefToExpand, vars, undefined,  {
        'CLICK_X': true,
        'CLICK_Y': true,
      });
  if (newHref != hrefToExpand) {
    if (!target.getAttribute(ORIG_HREF_ATTRIBUTE)) {
      target.setAttribute(ORIG_HREF_ATTRIBUTE, hrefToExpand);
    }
    target.setAttribute('href', newHref);
  }
}
function maybeExpandUrlParams ( ampdoc , e ) { const target = closestByTag ( dev ( ) . assertElement ( e . target ) , 'A' ) ; if ( ! target || ! target . href ) { return ; } const hrefToExpand = target . getAttribute ( ORIG_HREF_ATTRIBUTE ) || target . getAttribute ( 'href' ) ; if ( ! hrefToExpand ) { return ; } const vars = { 'CLICK_X' : ( ) => { return e . pageX ; } , 'CLICK_Y' : ( ) => { return e . pageY ; } , } ; const newHref = Services . urlReplacementsForDoc ( ampdoc ) . expandSync ( hrefToExpand , vars , undefined , { 'CLICK_X' : true , 'CLICK_Y' : true , } ) ; if ( newHref != hrefToExpand ) { if ( ! target . getAttribute ( ORIG_HREF_ATTRIBUTE ) ) { target . setAttribute ( ORIG_HREF_ATTRIBUTE , hrefToExpand ) ; } target . setAttribute ( 'href' , newHref ) ; } }
Handle click on links and replace variables in the click URL.
Handle click on links and replace variables in the click URL .
// Not a click on a link. /* opt_whitelist */ // For now we only allow to replace the click location vars // and nothing else. // NOTE: Addition to this whitelist requires additional review. // Store original value so that later clicks can be processed with // freshest values.