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

Function getOrCreateAdCid

src/ad-cid.js:32–68  ·  view source on GitHub ↗
(
  ampDoc,
  clientIdScope,
  opt_clientIdCookieName,
  opt_timeout
)

Source from the content-addressed store, hash-verified

30 * @return {!Promise<string|undefined>} A promise for a CID or undefined.
31 */
32export function getOrCreateAdCid(
33 ampDoc,
34 clientIdScope,
35 opt_clientIdCookieName,
36 opt_timeout
37) {
38 const timeout =
39 isNaN(opt_timeout) || opt_timeout == null ? 1000 : opt_timeout;
40 const cidPromise = Services.cidForDoc(ampDoc).then((cidService) => {
41 if (!cidService) {
42 return;
43 }
44 return cidService
45 .get(
46 {
47 scope: dev().assertString(clientIdScope),
48 createCookieIfNotPresent: true,
49 cookieName: opt_clientIdCookieName,
50 },
51 Promise.resolve(undefined)
52 )
53 .catch((error) => {
54 // Not getting a CID is not fatal.
55 dev().error('AD-CID', error);
56 return undefined;
57 });
58 });
59 // The CID should never be crucial for an ad. If it does not come within
60 // 1 second, assume it will never arrive.
61 return Services.timerFor(ampDoc.win)
62 .timeoutPromise(timeout, cidPromise, 'cid timeout')
63 .catch((error) => {
64 // Timeout is not fatal.
65 dev().warn('AD-CID', error);
66 return undefined;
67 });
68}

Callers 5

googlePageParametersFunction · 0.90
getAdUrlMethod · 0.90
getAdCidFunction · 0.85

Calls 9

devFunction · 0.90
catchMethod · 0.80
assertStringMethod · 0.80
resolveMethod · 0.80
timeoutPromiseMethod · 0.80
warnMethod · 0.80
thenMethod · 0.45
getMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected