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

Function extractAmpAnalyticsConfig

ads/google/a4a/utils.js:668–715  ·  view source on GitHub ↗
(a4a, responseHeaders)

Source from the content-addressed store, hash-verified

666 * @return {?JsonObject} config or null if invalid/missing.
667 */
668export function extractAmpAnalyticsConfig(a4a, responseHeaders) {
669 if (!responseHeaders.has(AMP_ANALYTICS_HEADER)) {
670 return null;
671 }
672 try {
673 const analyticsConfig = parseJson(
674 responseHeaders.get(AMP_ANALYTICS_HEADER)
675 );
676
677 const acUrls = analyticsConfig['url'];
678 const btrUrls = analyticsConfig['btrUrl'];
679 if (
680 (acUrls && !Array.isArray(acUrls)) ||
681 (btrUrls && !Array.isArray(btrUrls))
682 ) {
683 dev().error(
684 'AMP-A4A',
685 'Invalid analytics',
686 responseHeaders.get(AMP_ANALYTICS_HEADER)
687 );
688 }
689 const hasActiveViewRequests = Array.isArray(acUrls) && acUrls.length;
690 const hasBeginToRenderRequests = Array.isArray(btrUrls) && btrUrls.length;
691 if (!hasActiveViewRequests && !hasBeginToRenderRequests) {
692 return null;
693 }
694 const config = {
695 'transport': {'beacon': false, 'xhrpost': false},
696 'requests': {},
697 'triggers': {},
698 };
699 if (hasActiveViewRequests) {
700 generateActiveViewRequest(config, acUrls);
701 }
702 if (hasBeginToRenderRequests) {
703 generateBeginToRenderRequest(config, btrUrls);
704 }
705 return config;
706 } catch (err) {
707 dev().error(
708 'AMP-A4A',
709 'Invalid analytics',
710 err,
711 responseHeaders.get(AMP_ANALYTICS_HEADER)
712 );
713 }
714 return null;
715}
716
717/**
718 * @param {!JsonObject} config

Callers 3

test-utils.jsFile · 0.90
extractSizeMethod · 0.90
extractSizeMethod · 0.90

Calls 7

parseJsonFunction · 0.90
devFunction · 0.90
hasMethod · 0.45
getMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected