1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
BrowserSearchTelemetry: "resource:///modules/BrowserSearchTelemetry.jsm",
NetUtil: "resource://gre/modules/NetUtil.jsm",
SearchSERPTelemetry: "resource:///modules/SearchSERPTelemetry.jsm",
SearchUtils: "resource://gre/modules/SearchUtils.jsm",
Services: "resource://gre/modules/Services.jsm",
sinon: "resource://testing-common/Sinon.jsm",
TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.jsm",
});
const TESTS = [
{
title: "Google search access point",
trackingUrl:
"https://www.google.com/search?q=test&ie=utf-8&oe=utf-8&client=firefox-b-1-ab",
expectedSearchCountEntry: "google.in-content:sap:firefox-b-1-ab",
expectedAdKey: "google:sap",
adUrls: [
"https://www.googleadservices.com/aclk=foobar",
"https://www.googleadservices.com/pagead/aclk=foobar",
"https://www.google.com/aclk=foobar",
"https://www.google.com/pagead/aclk=foobar",
],
nonAdUrls: [
"https://www.googleadservices.com/?aclk=foobar",
"https://www.googleadservices.com/bar",
"https://www.google.com/image",
],
},
{
title: "Google search access point follow-on",
trackingUrl:
"https://www.google.com/search?client=firefox-b-1-ab&ei=EI_VALUE&q=test2&oq=test2&gs_l=GS_L_VALUE",
expectedSearchCountEntry: "google.in-content:sap-follow-on:firefox-b-1-ab",
},
{
title: "Google organic",
trackingUrl:
"https://www.google.com/search?source=hp&ei=EI_VALUE&q=test&oq=test&gs_l=GS_L_VALUE",
expectedSearchCountEntry: "google.in-content:organic:none",
expectedAdKey: "google:organic",
adUrls: ["https://www.googleadservices.com/aclk=foobar"],
nonAdUrls: ["https://www.googleadservices.com/?aclk=foobar"],
},
{
title: "Google organic UK",
trackingUrl:
"https://www.google.co.uk/search?source=hp&ei=EI_VALUE&q=test&oq=test&gs_l=GS_L_VALUE",
expectedSearchCountEntry: "google.in-content:organic:none",
},
{
title: "Yahoo organic",
trackingUrl:
"https://search.yahoo.com/search?p=test&fr=yfp-t&fp=1&toggle=1&cop=mss&ei=UTF-8",
expectedSearchCountEntry: "yahoo.in-content:organic:none",
},
{
title: "Yahoo organic UK",
trackingUrl:
"https://uk.search.yahoo.com/search?p=test&fr=yfp-t&fp=1&toggle=1&cop=mss&ei=UTF-8",
expectedSearchCountEntry: "yahoo.in-content:organic:none",
},
{
title: "Bing search access point",
trackingUrl: "https://www.bing.com/search?q=test&pc=MOZI&form=MOZLBR",
expectedSearchCountEntry: "bing.in-content:sap:MOZI",
expectedAdKey: "bing:sap",
adUrls: [
"https://www.bing.com/aclick?ld=foo",
"https://www.bing.com/fd/ls/GLinkPingPost.aspx?IG=bar&url=https%3A%2F%2Fwww.bing.com%2Faclick",
"https://www.bing.com/aclk?ld=foo",
"https://www.bing.com/fd/ls/GLinkPingPost.aspx?IG=bar&url=https%3A%2F%2Fwww.bing.com%2Faclk",
],
nonAdUrls: [
"https://www.bing.com/fd/ls/ls.gif?IG=foo",
"https://www.bing.com/fd/ls/l?IG=bar",
"https://www.bing.com/aclook?",
"https://www.bing.com/fd/ls/GLinkPingPost.aspx?IG=baz&url=%2Fvideos%2Fsearch%3Fq%3Dfoo",
],
},
{
setUp() {
Services.cookies.removeAll();
Services.cookies.add(
"www.bing.com",
"/",
"SRCHS",
"PC=MOZ",
false,
false,
false,
Date.now() + 1000 * 60 * 60,
{},
Ci.nsICookie.SAMESITE_NONE,
Ci.nsICookie.SCHEME_HTTPS
);
},
tearDown() {
Services.cookies.removeAll();
},
title: "Bing search access point follow-on",
trackingUrl:
"https://www.bing.com/search?q=test&qs=n&form=QBRE&sp=-1&pq=&sc=0-0&sk=&cvid=CVID_VALUE",
expectedSearchCountEntry: "bing.in-content:sap-follow-on:MOZ",
},
{
title: "Bing organic",
trackingUrl:
"https://www.bing.com/search?q=test&qs=n&form=QBLH&sp=-1&pq=&sc=0-0&sk=&cvid=CVID_VALUE",
expectedSearchCountEntry: "bing.in-content:organic:none",
expectedAdKey: "bing:organic",
adUrls: ["https://www.bing.com/aclick?ld=foo"],
nonAdUrls: ["https://www.bing.com/fd/ls/ls.gif?IG=foo"],
},
{
title: "DuckDuckGo search access point",
trackingUrl: "https://duckduckgo.com/?q=test&t=ffab",
expectedSearchCountEntry: "duckduckgo.in-content:sap:ffab",
expectedAdKey: "duckduckgo:sap",
adUrls: [
"https://duckduckgo.com/y.js?ad_provider=foo",
"https://duckduckgo.com/y.js?f=bar&ad_provider=foo",
"https://www.amazon.co.uk/foo?tag=duckduckgo-ffab-uk-32-xk",
],
nonAdUrls: [
"https://duckduckgo.com/?q=foo&t=ffab&ia=images&iax=images",
"https://duckduckgo.com/y.js?ifu=foo",
"https://improving.duckduckgo.com/t/bar",
],
},
{
title: "DuckDuckGo organic",
trackingUrl: "https://duckduckgo.com/?q=test&t=hi&ia=news",
expectedSearchCountEntry: "duckduckgo.in-content:organic:hi",
expectedAdKey: "duckduckgo:organic",
adUrls: ["https://duckduckgo.com/y.js?ad_provider=foo"],
nonAdUrls: ["https://duckduckgo.com/?q=foo&t=ffab&ia=images&iax=images"],
},
{
title: "Baidu search access point",
trackingUrl: "https://www.baidu.com/baidu?wd=test&tn=monline_7_dg&ie=utf-8",
expectedSearchCountEntry: "baidu.in-content:sap:monline_7_dg",
},
{
title: "Baidu search access point follow-on",
trackingUrl:
"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=monline_7_dg&wd=test2&oq=test&rsv_pq=RSV_PQ_VALUE&rsv_t=RSV_T_VALUE&rqlang=cn&rsv_enter=1&rsv_sug3=2&rsv_sug2=0&inputT=227&rsv_sug4=397",
expectedSearchCountEntry: "baidu.in-content:sap-follow-on:monline_7_dg",
},
{
title: "Baidu organic",
trackingUrl:
"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&ch=&tn=baidu&bar=&wd=test&rn=&oq=&rsv_pq=RSV_PQ_VALUE&rsv_t=RSV_T_VALUE&rqlang=cn",
expectedSearchCountEntry: "baidu.in-content:organic:baidu",
},
];
/**
* This function is primarily for testing the Ad URL regexps that are triggered
* when a URL is clicked on. These regexps are also used for the `with_ads`
* probe. However, we test the ad_clicks route as that is easier to hit.
*
* @param {string} serpUrl
* The url to simulate where the page the click came from.
* @param {string} adUrl
* The ad url to simulate being clicked.
* @param {string} [expectedAdKey]
* The expected key to be logged for the scalar. Omit if no scalar should be
* logged.
*/
async function testAdUrlClicked(serpUrl, adUrl, expectedAdKey) {
info(`Testing Ad URL: ${adUrl}`);
let channel = NetUtil.newChannel({
uri: NetUtil.newURI(adUrl),
triggeringPrincipal: Services.scriptSecurityManager.createContentPrincipal(
NetUtil.newURI(serpUrl),
{}
),
loadUsingSystemPrincipal: true,
});
SearchSERPTelemetry._contentHandler.observeActivity(
channel,
Ci.nsIHttpActivityObserver.ACTIVITY_TYPE_HTTP_TRANSACTION,
Ci.nsIHttpActivityObserver.ACTIVITY_SUBTYPE_TRANSACTION_CLOSE
);
// Since the content handler takes a moment to allow the channel information
// to settle down, wait the same amount of time here.
await new Promise(resolve => Services.tm.dispatchToMainThread(resolve));
const scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);
if (!expectedAdKey) {
Assert.ok(
!("browser.search.ad_clicks" in scalars),
"Should not have recorded an ad click"
);
} else {
TelemetryTestUtils.assertKeyedScalar(
scalars,
"browser.search.ad_clicks",
expectedAdKey,
1
);
}
}
add_task(async function setup() {
Services.prefs.setBoolPref(SearchUtils.BROWSER_SEARCH_PREF + "log", true);
await SearchSERPTelemetry.init();
sinon.stub(BrowserSearchTelemetry, "shouldRecordSearchCount").returns(true);
});
add_task(async function test_parsing_search_urls() {
for (const test of TESTS) {
info(`Running ${test.title}`);
if (test.setUp) {
test.setUp();
}
SearchSERPTelemetry.updateTrackingStatus(
{
getTabBrowser: () => {},
},
test.trackingUrl
);
const hs = Services.telemetry
.getKeyedHistogramById("SEARCH_COUNTS")
.snapshot();
Assert.ok(hs);
Assert.ok(
test.expectedSearchCountEntry in hs,
"The histogram must contain the correct key"
);
if ("adUrls" in test) {
for (const adUrl of test.adUrls) {
await testAdUrlClicked(test.trackingUrl, adUrl, test.expectedAdKey);
}
for (const nonAdUrls of test.nonAdUrls) {
await testAdUrlClicked(test.trackingUrl, nonAdUrls);
}
}
if (test.tearDown) {
test.tearDown();
}
}
});
|