@@ -18,28 +18,44 @@ const TAG = 'pixel';
18
18
* @param {string } src
19
19
* @param {?string= } referrerPolicy
20
20
* @param {string= } attributionSrc
21
+ * @param {(Element|./service/ampdoc-impl.AmpDoc)= } opt_elementOrAmpDoc Whether services are provided by an
22
+ * element.
21
23
* @return {!Element }
22
24
*/
23
- export function createPixel ( win , src , referrerPolicy , attributionSrc ) {
25
+ export function createPixel (
26
+ win ,
27
+ src ,
28
+ referrerPolicy ,
29
+ attributionSrc ,
30
+ opt_elementOrAmpDoc
31
+ ) {
24
32
// Caller need to verify window is not destroyed when creating pixel
25
33
if ( referrerPolicy && referrerPolicy !== 'no-referrer' ) {
26
34
user ( ) . error ( TAG , 'Unsupported referrerPolicy: %s' , referrerPolicy ) ;
27
35
}
28
36
29
37
return referrerPolicy === 'no-referrer'
30
- ? createNoReferrerPixel ( win , src , attributionSrc )
31
- : createImagePixel ( win , src , false , attributionSrc ) ;
38
+ ? createNoReferrerPixel ( win , src , attributionSrc , opt_elementOrAmpDoc )
39
+ : createImagePixel ( win , src , false , attributionSrc , opt_elementOrAmpDoc ) ;
32
40
}
33
41
34
42
/**
35
43
* @param {!Window } win
36
44
* @param {string } src
37
45
* @param {string= } attributionSrc
46
+ * @param {(Element|./service/ampdoc-impl.AmpDoc)= } opt_elementOrAmpDoc Whether services are provided by an
47
+ * element.
38
48
* @return {!Element }
39
49
*/
40
- function createNoReferrerPixel ( win , src , attributionSrc ) {
50
+ function createNoReferrerPixel ( win , src , attributionSrc , opt_elementOrAmpDoc ) {
41
51
if ( isReferrerPolicySupported ( ) ) {
42
- return createImagePixel ( win , src , true , attributionSrc ) ;
52
+ return createImagePixel (
53
+ win ,
54
+ src ,
55
+ true ,
56
+ attributionSrc ,
57
+ opt_elementOrAmpDoc
58
+ ) ;
43
59
} else {
44
60
// if "referrerPolicy" is not supported, use iframe wrapper
45
61
// to scrub the referrer.
@@ -52,7 +68,13 @@ function createNoReferrerPixel(win, src, attributionSrc) {
52
68
}
53
69
) ;
54
70
iframe . onload = ( ) => {
55
- createImagePixel ( iframe . contentWindow , src ) ;
71
+ createImagePixel (
72
+ iframe . contentWindow ,
73
+ src ,
74
+ undefined ,
75
+ undefined ,
76
+ opt_elementOrAmpDoc
77
+ ) ;
56
78
} ;
57
79
win . document . body . appendChild ( iframe ) ;
58
80
return iframe ;
@@ -64,9 +86,17 @@ function createNoReferrerPixel(win, src, attributionSrc) {
64
86
* @param {string } src
65
87
* @param {boolean= } noReferrer
66
88
* @param {string= } attributionSrc
89
+ * @param {(Element|./service/ampdoc-impl.AmpDoc)= } opt_elementOrAmpDoc Whether services are provided by an
90
+ * element.
67
91
* @return {!Image }
68
92
*/
69
- function createImagePixel ( win , src , noReferrer = false , attributionSrc ) {
93
+ function createImagePixel (
94
+ win ,
95
+ src ,
96
+ noReferrer = false ,
97
+ attributionSrc ,
98
+ opt_elementOrAmpDoc
99
+ ) {
70
100
const Image = WindowInterface . getImage ( win ) ;
71
101
const image = new Image ( ) ;
72
102
if ( noReferrer ) {
@@ -82,7 +112,8 @@ function createImagePixel(win, src, noReferrer = false, attributionSrc) {
82
112
const substituteVariables =
83
113
getAttributionReportingStatusUrlVariableRewriter (
84
114
win ,
85
- attributionReportingStatus
115
+ attributionReportingStatus ,
116
+ opt_elementOrAmpDoc
86
117
) ;
87
118
attributionSrc = substituteVariables ( attributionSrc ) ;
88
119
image . attributionSrc = attributionSrc ;
@@ -93,7 +124,8 @@ function createImagePixel(win, src, noReferrer = false, attributionSrc) {
93
124
}
94
125
const substituteVariables = getAttributionReportingStatusUrlVariableRewriter (
95
126
win ,
96
- attributionReportingStatus
127
+ attributionReportingStatus ,
128
+ opt_elementOrAmpDoc
97
129
) ;
98
130
src = substituteVariables ( src ) ;
99
131
image . src = src ;
@@ -113,13 +145,21 @@ function isReferrerPolicySupported() {
113
145
/**
114
146
* @param {!Window } win
115
147
* @param {string= } status
148
+ * @param {(Element|./service/ampdoc-impl.AmpDoc)= } opt_elementOrAmpDoc Whether services are provided by an
149
+ * element.
116
150
* @return {function(string): string }
117
151
*/
118
- function getAttributionReportingStatusUrlVariableRewriter ( win , status ) {
152
+ function getAttributionReportingStatusUrlVariableRewriter (
153
+ win ,
154
+ status ,
155
+ opt_elementOrAmpDoc
156
+ ) {
119
157
const substitutionFunctions = {
120
158
'ATTRIBUTION_REPORTING_STATUS' : ( ) => status ,
121
159
} ;
122
- const replacements = Services . urlReplacementsForDoc ( win . document ) ;
160
+ const replacements = Services . urlReplacementsForDoc (
161
+ opt_elementOrAmpDoc || win . document
162
+ ) ;
123
163
const allowlist = {
124
164
'ATTRIBUTION_REPORTING_STATUS' : true ,
125
165
} ;
0 commit comments