From 9bd52ba7d08fba0181fd97ecea95c0f316a9e9c1 Mon Sep 17 00:00:00 2001 From: jonkeller Date: Thu, 29 Aug 2019 14:53:01 -0400 Subject: [PATCH 1/8] Adds AdSense utility functions --- .../utils/resource-classification.js | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/lighthouse-plugin-publisher-ads/utils/resource-classification.js b/lighthouse-plugin-publisher-ads/utils/resource-classification.js index 8c073248..29ef2613 100644 --- a/lighthouse-plugin-publisher-ads/utils/resource-classification.js +++ b/lighthouse-plugin-publisher-ads/utils/resource-classification.js @@ -69,6 +69,29 @@ function isGpt(url) { return isGptTag(url) || isImplTag(url); } +/** + * Checks if the url is loading an AdSense script. + * @param {URL|string} url + * @return {boolean} + */ +function isAdSenseTag(url) { + const {host, pathname} = toURL(url); + const matchesHost = [ + 'pagead2.googlesyndication.com'].includes(host); + const matchesPath = + ['pagead/js/adsbygoogle.js'].includes(pathname); + return matchesHost && matchesPath; +} + +/** + * Checks if the url is loading an AdSense script. + * @param {URL} url + * @return {boolean} + */ +function isAdSense(url) { + return isAdSenseTag(url); +} + /** * Checks if str contains at least one provided substring. * @param {string} str @@ -88,9 +111,23 @@ function isGptAdRequest(request) { if (!request) return false; const url = new URL(request.url); return ( - url.pathname === '/gampad/ads' && - url.host === 'securepubads.g.doubleclick.net' && - request.resourceType === 'XHR' + url.pathname === '/gampad/ads' && + url.host === 'securepubads.g.doubleclick.net' && + request.resourceType === 'XHR' + ); +} + +/** + * Checks if a network request is an AdSense ad request. + * @param {LH.Artifacts.NetworkRequest} request + * @return {boolean} + */ +function isAdSenseAdRequest(request) { + if (!request) return false; + const url = new URL(request.url); + return ( + url.pathname === 'pagead/ads' && + url.host === 'googleads.g.doubleclick.net' ); } @@ -168,6 +205,14 @@ function isGptIframe(iframe) { return /(^google_ads_iframe_)/.test(iframe.id); } +/** + * @param {Artifacts['IFrameElement']} iframe + * @return {boolean} + */ +function isAdSenseIframe(iframe) { + return /(^google_ads_frame)/.test(iframe.id); +} + /** * Removes the query string from the URL. * @param {string} url @@ -201,14 +246,18 @@ module.exports = { isBidRequest, isGoogleAds, isGptAdRequest, + isAdSenseAdRequest, hasImpressionPath, isGpt, isGptTag, isImplTag, + isAdSense, + isAdSenseTag, containsAnySubstring, getHeaderBidder, isStaticRequest, isGptIframe, + isAdSenseIframe, getAbbreviatedUrl, trimUrl, }; From aa64d466c2e827a3ccfc2bbbdfcee6a55f14405c Mon Sep 17 00:00:00 2001 From: jonkeller Date: Wed, 4 Sep 2019 11:27:52 -0400 Subject: [PATCH 2/8] LoadsGptOverHttps to LoadsAdTagOverHttps --- .../audits/gpt-bids-parallel.js | 4 +- ...er-https.js => loads-ad-tag-over-https.js} | 52 +++--- .../audits/loads-gpt-from-sgdn.js | 2 +- .../messages/common-strings.js | 2 - .../messages/en-US.json | 20 +-- .../messages/locales/en-XL.json | 13 +- lighthouse-plugin-publisher-ads/plugin.js | 4 +- ...est.js => loads-ad-tag-over-https_test.js} | 124 ++++++++++----- .../utils/resource-classification.js | 148 ++++++++++-------- 9 files changed, 216 insertions(+), 153 deletions(-) rename lighthouse-plugin-publisher-ads/audits/{loads-gpt-over-https.js => loads-ad-tag-over-https.js} (61%) rename lighthouse-plugin-publisher-ads/test/audits/{loads-gpt-over-https_test.js => loads-ad-tag-over-https_test.js} (68%) diff --git a/lighthouse-plugin-publisher-ads/audits/gpt-bids-parallel.js b/lighthouse-plugin-publisher-ads/audits/gpt-bids-parallel.js index 0b2777ad..fef27e8c 100644 --- a/lighthouse-plugin-publisher-ads/audits/gpt-bids-parallel.js +++ b/lighthouse-plugin-publisher-ads/audits/gpt-bids-parallel.js @@ -18,7 +18,7 @@ const {auditNotApplicable} = require('../messages/common-strings'); const {Audit} = require('lighthouse'); const {getCriticalGraph} = require('../utils/graph'); const {getTimingsByRecord} = require('../utils/network-timing'); -const {isImplTag, isBidRequest, getAbbreviatedUrl, getHeaderBidder} = require('../utils/resource-classification'); +const {isGptImplTag, isBidRequest, getAbbreviatedUrl, getHeaderBidder} = require('../utils/resource-classification'); /** @typedef {LH.Artifacts.NetworkRequest} NetworkRequest */ /** @typedef {LH.Gatherer.Simulation.NodeTiming} NodeTiming */ @@ -73,7 +73,7 @@ class GptBidsInParallel extends Audit { const trace = artifacts.traces[Audit.DEFAULT_PASS]; const network = await NetworkRecords.request(devtoolsLog, context); - const pubadsImpl = network.find((r) => isImplTag(r.url)); + const pubadsImpl = network.find((r) => isGptImplTag(r.url)); if (!pubadsImpl) { return auditNotApplicable.NoTag; } diff --git a/lighthouse-plugin-publisher-ads/audits/loads-gpt-over-https.js b/lighthouse-plugin-publisher-ads/audits/loads-ad-tag-over-https.js similarity index 61% rename from lighthouse-plugin-publisher-ads/audits/loads-gpt-over-https.js rename to lighthouse-plugin-publisher-ads/audits/loads-ad-tag-over-https.js index 74408cae..19201f5e 100644 --- a/lighthouse-plugin-publisher-ads/audits/loads-gpt-over-https.js +++ b/lighthouse-plugin-publisher-ads/audits/loads-ad-tag-over-https.js @@ -17,37 +17,41 @@ const NetworkRecords = require('lighthouse/lighthouse-core/computed/network-reco const util = require('util'); const {auditNotApplicable} = require('../messages/common-strings'); const {Audit} = require('lighthouse'); -const {isGptTag} = require('../utils/resource-classification'); +const {isAdTag} = require('../utils/resource-classification'); const {URL} = require('url'); const UIStrings = { - title: 'GPT tag is loaded over HTTPS', - failureTitle: 'Load GPT over HTTPS', - description: 'For privacy and security, always load GPT over HTTPS. ' + - 'Insecure pages should explicitly request the GPT script securely. Example:' + - '`