Skip to content

Commit

Permalink
analytics & ads macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Micajuine Ho committed Sep 8, 2020
1 parent 63848d3 commit 1c69be2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
12 changes: 9 additions & 3 deletions extensions/amp-a4a/0.1/amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,11 @@ export class AmpA4A extends AMP.BaseElement {

return /** @type {!Promise<?string>} */ (this.getAdUrl(
{consentState, consentString, gdprApplies},
this.tryExecuteRealTimeConfig_(consentState, consentString)
this.tryExecuteRealTimeConfig_(
consentState,
consentString,
consentMetadata
)
));
})
// This block returns the (possibly empty) response to the XHR request.
Expand Down Expand Up @@ -2209,15 +2213,17 @@ export class AmpA4A extends AMP.BaseElement {
* the rtc-config attribute on the amp-ad element, warn.
* @param {?CONSENT_POLICY_STATE} consentState
* @param {?string} consentString
* @param {?Object} consentMetadata
* @return {Promise<!Array<!rtcResponseDef>>|undefined}
*/
tryExecuteRealTimeConfig_(consentState, consentString) {
tryExecuteRealTimeConfig_(consentState, consentString, consentMetadata) {
if (!!AMP.RealTimeConfigManager) {
try {
return new AMP.RealTimeConfigManager(this).maybeExecuteRealTimeConfig(
this.getCustomRealTimeConfigMacros_(),
consentState,
consentString
consentString,
consentMetadata
);
} catch (err) {
user().error(TAG, 'Could not perform Real Time Config.', err);
Expand Down
14 changes: 13 additions & 1 deletion extensions/amp-a4a/0.1/real-time-config-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export class RealTimeConfigManager {

/** @private {?string} */
this.consentString_ = null;

/** @private {?Object} */
this.consentMetadata_ = null;
}

/**
Expand Down Expand Up @@ -166,15 +169,22 @@ export class RealTimeConfigManager {
* substitutions available to use.
* @param {?CONSENT_POLICY_STATE} consentState
* @param {?string} consentString
* @param {?Object} consentMetadata
* @return {Promise<!Array<!rtcResponseDef>>|undefined}
* @visibleForTesting
*/
maybeExecuteRealTimeConfig(customMacros, consentState, consentString) {
maybeExecuteRealTimeConfig(
customMacros,
consentState,
consentString,
consentMetadata
) {
if (!this.validateRtcConfig_(this.a4aElement_.element)) {
return;
}
this.consentState_ = consentState;
this.consentString_ = consentString;
this.consentMetadata_ = consentMetadata;
this.modifyRtcConfigForConsentStateSettings();
customMacros = this.assignMacros(customMacros);
this.rtcStartTime_ = Date.now();
Expand Down Expand Up @@ -276,6 +286,8 @@ export class RealTimeConfigManager {
macros['TIMEOUT'] = () => this.rtcConfig_.timeoutMillis;
macros['CONSENT_STATE'] = () => this.consentState_;
macros['CONSENT_STRING'] = () => this.consentString_;
macros['CONSENT_METADATA'] = (key) =>
this.consentMetadata_ ? this.consentMetadata_[key] : null;
return macros;
}

Expand Down
20 changes: 19 additions & 1 deletion extensions/amp-analytics/0.1/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getActiveExperimentBranches,
getExperimentBranch,
} from '../../../src/experiments';
import {getConsentPolicyState} from '../../../src/consent';
import {getConsentMetadata, getConsentPolicyState} from '../../../src/consent';
import {
getServiceForDoc,
getServicePromiseForDoc,
Expand Down Expand Up @@ -273,6 +273,8 @@ export class VariableService {
'COOKIE': (name) =>
cookieReader(this.ampdoc_.win, dev().assertElement(element), name),
'CONSENT_STATE': getConsentStateStr(element),
'CONSENT_METADATA': (key) =>
getConsentMetadataValue(element, dev().assertString(key)),
};
const perfMacros = isInFie(element)
? {}
Expand Down Expand Up @@ -525,6 +527,22 @@ function getConsentStateStr(element) {
});
}

/**
* Get the associated value from the resolved consent metadata object
* @param {!Element} element
* @param {string} key
* @return {!Promise<?Object>}
*/
function getConsentMetadataValue(element, key) {
// Get the metadata using the default policy id
return getConsentMetadata(element).then((consentMetadata) => {
if (!consentMetadata) {
return null;
}
return consentMetadata[key];
});
}

/**
* Converts string to boolean
* @param {string} str
Expand Down
2 changes: 1 addition & 1 deletion src/consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function getConsentPolicyInfo(element, policyId) {
* @param {string} policyId
* @return {!Promise<?Object|undefined>}
*/
export function getConsentMetadata(element, policyId) {
export function getConsentMetadata(element, policyId = 'default') {
// Return the stored consent metadata.
return Services.consentPolicyServiceForDocOrNull(element).then(
(consentPolicy) => {
Expand Down

0 comments on commit 1c69be2

Please sign in to comment.