An error has occurred

{{terminalError}}

This is your first complimentary story

Explore emerging technology with an MIT Technology Review subscription.

const targetAnchorsClass = 'a.pn-template__cta-link'; const capturedAngularElementId = 'metered-article-1-custom-field'; changeAnchorParams(targetAnchorsClass, capturedAngularElementId); function changeAnchorParams(anchorClass, angularId) { const domElement = document.getElementById(angularId); const ngElement = angular.element(domElement); ngElement.scope().$watchCollection('params', (newParams, oldParams) => { if (newParams.url === oldParams.url) return; const userUrl = newParams.url; const paramsToReplace = (new URL(userUrl)).searchParams; if (0 === [...paramsToReplace.keys()].length) return; const anchorsToChange = document.querySelectorAll(anchorClass); if (0 === anchorsToChange.length) return; anchorsToChange.forEach((anchor) => { const anchorHref = new URL(anchor.href); const anchorSearchParams = anchorHref.searchParams; [...paramsToReplace.keys()].forEach((param) => { if (! param.startsWith('utm_') && ! param.startsWith('itm_')) return; if (anchorSearchParams.has(param)) return; anchorSearchParams.set(param, paramsToReplace.get(param)); }); const newAnchorHref = anchorHref.toString(); anchor.href = decodeURIComponent(newAnchorHref); }); }); }