Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer.cc

Issue 2798953002: [PageLoadMetrics] Keep track of Ad Sizes on Pages (Closed)
Patch Set: Address comments from PS11-13 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/page_load_metrics/observers/ads_page_load_metrics_obser ver.h"
6
7 #include <string>
8
9 #include "base/feature_list.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_util.h"
12 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h"
13 #include "content/public/browser/navigation_handle.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "url/gurl.h"
17
18 namespace {
19
20 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT};
21
22 bool FrameIsAd(content::NavigationHandle* navigation_handle) {
23 int frame_tree_node_id = navigation_handle->GetFrameTreeNodeId();
24 content::RenderFrameHost* current_frame_host =
25 navigation_handle->GetWebContents()->FindFrameByFrameTreeNodeId(
Bryan McQuade 2017/04/14 19:19:23 is it possible for FindFrameByFrameTreeNodeId to r
Charlie Harrison 2017/04/14 20:16:26 I think we should be able to DCHECK this especiall
Bryan McQuade 2017/04/17 22:00:04 actually, can we just call navigation_handle->GetR
jkarlin 2017/04/24 17:27:58 Implemented jam's suggestion and dcheck the pointe
26 frame_tree_node_id);
jam 2017/04/17 15:41:55 these two lines can just be navigation_handle->Get
jkarlin 2017/04/24 17:27:58 Done.
27 const std::string& name = current_frame_host->GetFrameName();
28 const GURL& url = navigation_handle->GetURL();
29
30 // Because sub-resource filtering isn't always enabled, and doesn't work
31 // well in monitoring mode (no CSS enforcement), it's difficult to identify
32 // ads. Google ads are prevalent and easy to track, so we'll start by
33 // tracking those. Note that the frame name can be very large, so be careful
34 // to avoid full string searches if possible.
35 // TODO(jkarlin): Track other ad networks that are easy to identify.
36 return base::StartsWith(name, "google_ads_iframe",
37 base::CompareCase::SENSITIVE) ||
38 base::StartsWith(name, "google_ads_frame",
39 base::CompareCase::SENSITIVE) ||
40 (url.host_piece() == "tpc.googlesyndication.com" &&
41 base::StartsWith(url.path_piece(), "/safeframe",
42 base::CompareCase::SENSITIVE));
43 }
44
45 } // namespace
46
47 // static
48 std::unique_ptr<AdsPageLoadMetricsObserver>
49 AdsPageLoadMetricsObserver::CreateIfNeeded() {
50 if (!base::FeatureList::IsEnabled(kAdsFeature))
51 return nullptr;
52 return base::MakeUnique<AdsPageLoadMetricsObserver>();
53 }
54
55 AdsPageLoadMetricsObserver::AdsPageLoadMetricsObserver() = default;
56 AdsPageLoadMetricsObserver::~AdsPageLoadMetricsObserver() = default;
57
58 page_load_metrics::PageLoadMetricsObserver::ObservePolicy
59 AdsPageLoadMetricsObserver::OnCommit(
60 content::NavigationHandle* navigation_handle) {
61 DCHECK(ad_frames_data_.empty());
Charlie Harrison 2017/04/14 20:16:26 nit: include base/logging for DCHECK
jkarlin 2017/04/24 17:27:58 Done.
62
63 // The main frame is never considered an ad.
64 ad_frames_data_[navigation_handle->GetFrameTreeNodeId()] = nullptr;
65 ProcessOngoingNavigationResources(navigation_handle->GetFrameTreeNodeId());
66 return CONTINUE_OBSERVING;
67 }
68
69 page_load_metrics::PageLoadMetricsObserver::ObservePolicy
70 AdsPageLoadMetricsObserver::OnCommitSubFrame(
71 content::NavigationHandle* navigation_handle) {
72 FrameTreeNodeId frame_tree_node_id = navigation_handle->GetFrameTreeNodeId();
73 content::RenderFrameHost* parent_frame_host =
74 navigation_handle->GetRenderFrameHost()->GetParent();
75 DCHECK(parent_frame_host);
76
77 bool top_level_subframe = !parent_frame_host->GetParent();
78
79 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id);
80 if (id_and_data != ad_frames_data_.end()) {
81 // An existing subframe is navigating again.
82 if (id_and_data->second) {
83 // The subframe was an ad to begin with, keep tracking it as an ad.
84 ProcessOngoingNavigationResources(frame_tree_node_id);
85 return CONTINUE_OBSERVING;
86 }
87 // This frame was previously not an ad, process it as usual. If it had
Bryan McQuade 2017/04/14 19:19:23 do we expect repeat navs for ads? if not, it might
Charlie Harrison 2017/04/14 20:16:26 +1
jkarlin 2017/04/24 17:27:58 Certainly doable. What would we do with this infor
Bryan McQuade 2017/04/24 19:28:22 I expect most people looking at the rest of the hi
88 // any child frames that were ads, those will still be recorded.
89 } else if (top_level_subframe) {
90 top_level_subframe_count_ += 1;
91 }
92
93 // Determine who the parent frame's ad ancestor is.
94 const auto& parent_id_and_data =
95 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId());
96 DCHECK(parent_id_and_data != ad_frames_data_.end());
97 AdFrameData* ad_data = parent_id_and_data->second;
98
99 if (!ad_data && FrameIsAd(navigation_handle)) {
100 // This frame is not nested within an ad frame but is itself an ad.
101 ad_frames_data_storage_.push_back(AdFrameData());
Bryan McQuade 2017/04/14 19:19:23 i'm not deeply familiar with it, but this looks li
jkarlin 2017/04/24 17:27:59 Done.
102 ad_data = &ad_frames_data_storage_.back();
103 }
104
105 ad_frames_data_[frame_tree_node_id] = ad_data;
106
107 if (top_level_subframe && ad_data)
108 top_level_ad_frame_count_ += 1;
109
110 ProcessOngoingNavigationResources(frame_tree_node_id);
111 return CONTINUE_OBSERVING;
112 }
113
114 page_load_metrics::PageLoadMetricsObserver::ObservePolicy
115 AdsPageLoadMetricsObserver::FlushMetricsOnAppEnterBackground(
116 const page_load_metrics::PageLoadTiming& timing,
117 const page_load_metrics::PageLoadExtraInfo& extra_info) {
118 // The browser may come back, but there is no guarantee. To be safe, record
119 // what we have now and ignore future changes to this navigation.
120 if (extra_info.did_commit)
121 RecordHistograms();
122
123 return STOP_OBSERVING;
124 }
125
126 void AdsPageLoadMetricsObserver::OnLoadedResource(
127 const page_load_metrics::ExtraRequestInfo& extra_request_info) {
128 const auto& id_and_data =
129 ad_frames_data_.find(extra_request_info.frame_tree_node_id);
130 if (id_and_data == ad_frames_data_.end()) {
131 // This resouce is for a frame that hasn't yet committed. It must be the
132 // main document for the frame. Hold onto it and once it commits we'll run
133 // it in ProcessOngoingNavigationResources.
134 // TODO(jkarlin): Plumb the resource type through and DCHECK that the type
135 // is document.
136
137 // Because ExtraRequestInfo doesn't have a default constructor, use insert
138 // to add the entry to the map.
139 auto it_and_success = ongoing_navigation_resources_.insert(std::make_pair(
140 extra_request_info.frame_tree_node_id, extra_request_info));
141 if (!it_and_success.second)
Bryan McQuade 2017/04/17 22:00:04 should we dcheck that no entry already exists for
jkarlin 2017/04/24 17:27:59 Yes, that can happen hence the lack of dcheck.
142 it_and_success.first->second = extra_request_info;
143 return;
144 }
145
146 page_bytes_ += extra_request_info.raw_body_bytes;
147 if (!extra_request_info.was_cached)
148 uncached_page_bytes_ += extra_request_info.raw_body_bytes;
149
150 // Determine if the frame (or its ancestor) is an ad, if so attribute the
151 // bytes to the highest ad ancestor.
152 AdFrameData* ancestor_data = id_and_data->second;
153
154 if (ancestor_data) {
155 ancestor_data->frame_bytes += extra_request_info.raw_body_bytes;
156 if (!extra_request_info.was_cached) {
157 ancestor_data->frame_bytes_uncached += extra_request_info.raw_body_bytes;
158 }
159 }
160 }
161
162 void AdsPageLoadMetricsObserver::OnComplete(
163 const page_load_metrics::PageLoadTiming& timing,
164 const page_load_metrics::PageLoadExtraInfo& info) {
165 RecordHistograms();
166 }
167
168 void AdsPageLoadMetricsObserver::RecordHistograms() {
169 if (page_bytes_ == 0)
170 return;
171
172 size_t total_ad_frame_bytes = 0;
173 size_t uncached_ad_frame_bytes = 0;
174
175 for (const AdFrameData& ad_frame_data : ad_frames_data_storage_) {
176 total_ad_frame_bytes += ad_frame_data.frame_bytes;
177 uncached_ad_frame_bytes += ad_frame_data.frame_bytes_uncached;
178
179 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes",
Bryan McQuade 2017/04/18 17:11:32 wdyt about triyng to use a naming scheme more cons
jkarlin 2017/04/24 17:27:59 Good idea, done.
180 ad_frame_data.frame_bytes);
181 PAGE_BYTES_HISTOGRAM(
182 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork",
183 ad_frame_data.frame_bytes_uncached);
184 if (ad_frame_data.frame_bytes > 0) {
185 UMA_HISTOGRAM_PERCENTAGE(
186 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork",
Bryan McQuade 2017/04/18 17:11:33 these percentage histograms have historically been
jkarlin 2017/04/24 17:27:58 I think it's useful to be able to make statements
187 ad_frame_data.frame_bytes_uncached * 100 / ad_frame_data.frame_bytes);
188 }
189 }
190
191 UMA_HISTOGRAM_BOOLEAN("PageLoad.Clients.Ads.Google.PageHasNoAds",
Bryan McQuade 2017/04/18 17:11:33 could we instead have a single counter for number
jkarlin 2017/04/24 17:27:59 Done.
192 ad_frames_data_storage_.empty());
193
194 // Don't post UMA for pages that don't have ads.
195 if (ad_frames_data_storage_.empty())
Bryan McQuade 2017/04/18 17:11:32 can we move the earlier histo that reports ad coun
jkarlin 2017/04/24 17:27:58 Done.
196 return;
197
198 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.TopLevelSubFrameCount",
199 top_level_subframe_count_);
200 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.TopLevelAdFrameCount",
Bryan McQuade 2017/04/18 17:11:33 since you have 'Ads.Google.AdFrameCount' below, co
jkarlin 2017/04/24 17:27:59 Done, though I didn't use proper suffixes in the h
201 top_level_ad_frame_count_);
202
203 DCHECK_LT(0, top_level_subframe_count_); // Because ad frames isn't empty.
204 UMA_HISTOGRAM_PERCENTAGE(
205 "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames",
206 top_level_ad_frame_count_ * 100 / top_level_subframe_count_);
207
208 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes",
Bryan McQuade 2017/04/18 17:11:32 here too if we use the 'Bytes.PerAdFrame.Total' ab
jkarlin 2017/04/24 17:27:59 Done.
209 total_ad_frame_bytes);
210
211 PAGE_BYTES_HISTOGRAM(
212 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames",
Bryan McQuade 2017/04/18 17:11:33 Bytes.NonAdFrames.Total?
jkarlin 2017/04/24 17:27:59 Done.
213 page_bytes_ - total_ad_frame_bytes);
214
215 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PageBytes",
Bryan McQuade 2017/04/18 17:11:32 Bytes.Total?
jkarlin 2017/04/24 17:27:59 Done.
216 page_bytes_);
217 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork",
Bryan McQuade 2017/04/18 17:11:32 Bytes.Network?
jkarlin 2017/04/24 17:27:58 Done.
218 uncached_page_bytes_);
219 if (page_bytes_) {
220 UMA_HISTOGRAM_PERCENTAGE(
221 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames",
222 total_ad_frame_bytes * 100 / page_bytes_);
223 }
224
225 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.AdFrameCount",
226 ad_frames_data_storage_.size());
227
228 PAGE_BYTES_HISTOGRAM(
229 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork",
Bryan McQuade 2017/04/18 17:11:32 Bytes.AllAdFrames.Total?
jkarlin 2017/04/24 17:27:58 I think you mean Bytes.AllAdFrames.Network. Done.
230 uncached_ad_frame_bytes);
231
232 if (total_ad_frame_bytes) {
233 UMA_HISTOGRAM_PERCENTAGE(
234 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork",
235 uncached_ad_frame_bytes * 100 / total_ad_frame_bytes);
236 }
237 if (uncached_page_bytes_ > 0) {
238 UMA_HISTOGRAM_PERCENTAGE(
239 "PageLoad.Clients.Ads.Google.Bytes."
240 "PercentPageNetworkBytesFromAllAdFrames",
241 uncached_ad_frame_bytes * 100 / uncached_page_bytes_);
242 }
243 }
244
245 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResources(
246 FrameTreeNodeId frame_tree_node_id) {
247 const auto& frame_id_and_request =
248 ongoing_navigation_resources_.find(frame_tree_node_id);
249 if (frame_id_and_request == ongoing_navigation_resources_.end())
250 return;
251 OnLoadedResource(frame_id_and_request->second);
Bryan McQuade 2017/04/17 22:00:04 can we instead create a method ProcessLoadedResour
jkarlin 2017/04/24 17:27:58 Done.
252 ongoing_navigation_resources_.erase(frame_id_and_request);
253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698