MCPcopy
hub / github.com/ampproject/amphtml / maybeTrackImpression

Function maybeTrackImpression

src/impression.js:58–100  ·  view source on GitHub ↗
(win)

Source from the content-addressed store, hash-verified

56 * @param {!Window} win
57 */
58export function maybeTrackImpression(win) {
59 const deferred = new Deferred();
60 const {promise, resolve: resolveImpression} = deferred;
61
62 trackImpressionPromise = Services.timerFor(win)
63 .timeoutPromise(TIMEOUT_VALUE, promise, 'TrackImpressionPromise timeout')
64 .catch((error) => {
65 dev().warn('IMPRESSION', error);
66 });
67
68 const viewer = Services.viewerForDoc(win.document.documentElement);
69 const isTrustedViewerPromise = viewer.isTrustedViewer();
70 const isTrustedReferrerPromise = viewer
71 .getReferrerUrl()
72 .then((referrer) => isTrustedReferrer(referrer));
73 Promise.all([isTrustedViewerPromise, isTrustedReferrerPromise]).then(
74 (results) => {
75 const isTrustedViewer = results[0];
76 const isTrustedReferrer = results[1];
77 // Enable the feature in the case of trusted viewer,
78 // or trusted referrer
79 // or with experiment turned on
80 if (
81 !isTrustedViewer &&
82 !isTrustedReferrer &&
83 !isExperimentOn(win, 'alp')
84 ) {
85 resolveImpression();
86 return;
87 }
88
89 const replaceUrlPromise = handleReplaceUrl(win);
90 const clickUrlPromise = handleClickUrl(win);
91
92 Promise.all([replaceUrlPromise, clickUrlPromise]).then(
93 () => {
94 resolveImpression();
95 },
96 () => {}
97 );
98 }
99 );
100}
101
102/**
103 * Signal that impression tracking is not relevant in this environment.

Callers 3

test-impression.jsFile · 0.90
bootstrapFunction · 0.90
setupMethod · 0.90

Calls 11

devFunction · 0.90
isTrustedReferrerFunction · 0.85
handleReplaceUrlFunction · 0.85
handleClickUrlFunction · 0.85
catchMethod · 0.80
timeoutPromiseMethod · 0.80
warnMethod · 0.80
isExperimentOnFunction · 0.50
isTrustedViewerMethod · 0.45
thenMethod · 0.45
getReferrerUrlMethod · 0.45

Tested by

no test coverage detected