Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 1ff43e1

Browse files
jeffkaufmancrowell
authored andcommitted
insert-ga: accept dc.js as a synonym for ga.js
Fixes #1338
1 parent 68af83e commit 1ff43e1

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎net/instaweb/rewriter/insert_ga_filter.cc‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ void InsertGAFilter::StartElementImpl(HtmlElement* element) {
221221
// some false positives; the later check is more thorough.
222222
InsertGAFilter::AnalyticsStatus InsertGAFilter::FindSnippetInScript(
223223
const GoogleString& s) {
224+
// dc.js is a synonym for old-style ga.js
224225
if (!seen_sync_ga_js_ &&
225-
s.find("google-analytics.com/ga.js") != GoogleString::npos) {
226+
(s.find("google-analytics.com/ga.js") != GoogleString::npos ||
227+
s.find("stats.g.doubleclick.net/dc.js") != GoogleString::npos)) {
226228
// The synchronous snippet has two parts: first one with
227229
// [google-analytics.com/ga.js] (no initial dot) and then a later one with
228230
// ga_id, _getTracker, and _trackPageview. Track that we've seen what is
@@ -236,7 +238,8 @@ InsertGAFilter::AnalyticsStatus InsertGAFilter::FindSnippetInScript(
236238
}
237239
if (s.find(".google-analytics.com/urchin.js") != GoogleString::npos) {
238240
return kUnusableSnippetFound; // urchin.js is too old.
239-
} else if (s.find(".google-analytics.com/ga.js") != GoogleString::npos) {
241+
} else if (s.find(".google-analytics.com/ga.js") != GoogleString::npos ||
242+
s.find("stats.g.doubleclick.net/dc.js") != GoogleString::npos) {
240243
// With the async snippet there is one part that first loads ga.js
241244
// (using [.google-analytics.com/ga.js], with initial dot) and then has the
242245
// ga_id (which we checked for above).

‎net/instaweb/rewriter/insert_ga_filter_test.cc‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,36 @@ const char kAsyncGA[] =
9999
"})();"
100100
"</script>";
101101

102+
const char kSynchronousDC[] =
103+
"%s"
104+
"<script>"
105+
" var gaJsHost = ((\"https:\" == document.location.protocol) ?"
106+
" \"https://\" : \"http://\");"
107+
" document.write(unescape(\"%%3Cscript src='\" + gaJsHost +"
108+
" \"stats.g.doubleclick.net/dc.js'"
109+
" type='text/javascript'%%3E%%3C/script%%3E\"));"
110+
"</script>"
111+
"<script>%s"
112+
" try { var pageTracker = _gat._getTracker(\"%s\");"
113+
" pageTracker._trackPageview(); } catch(err) {}"
114+
"</script>";
115+
116+
const char kAsyncDC[] =
117+
"%s"
118+
"<script type='text/javascript'>document.write('another script');</script>"
119+
"<script>%s"
120+
"var _gaq = _gaq || [];"
121+
"_gaq.push(['_setAccount', '%s']);"
122+
"_gaq.push(['_trackPageview']);"
123+
"(function() {"
124+
" var ga = document.createElement('script');"
125+
" ga.src = ('https:' == document.location.protocol ?"
126+
" 'https://' : 'http://') +"
127+
" 'stats.g.doubleclick.net/dc.js'';"
128+
" ga.setAttribute('async', 'true');"
129+
" document.documentElement.firstChild.appendChild(ga);"
130+
"})();"
131+
"</script>";
102132

103133
const char kAsyncGAPart1[] =
104134
"<script type='text/javascript'>document.write('another script');</script>"
@@ -553,6 +583,17 @@ TEST_F(InsertGAFilterTest, SynchronousGACustomVarExperiment) {
553583
ValidateExpected("extend sync ga.js for cv experiment", input, output);
554584
}
555585

586+
TEST_F(InsertGAFilterTest, SynchronousDCCustomVarExperiment) {
587+
// dc.js version of SynchronousGACustomVarExperiment.
588+
GoogleString experiment_string;
589+
SetUpCustomVarExperiment(false, &experiment_string);
590+
GoogleString input = StringPrintf(kSynchronousDC, "", "", kGaId);
591+
GoogleString output = StringPrintf(kSynchronousDC, "<head/>", StringPrintf(
592+
kGAExperimentSnippet, kGASpeedTracking, 4,
593+
experiment_string.c_str()).c_str(), kGaId);
594+
ValidateExpected("extend sync dc.js for cv experiment", input, output);
595+
}
596+
556597
TEST_F(InsertGAFilterTest, SynchronousGAContentExperiment) {
557598
// Show that we can add content experiment tracking to existing synchronous
558599
// ga.js usage.
@@ -592,6 +633,17 @@ TEST_F(InsertGAFilterTest, AsynchronousGACustomVarExperiment) {
592633
ValidateExpected("extend async ga.js for cv experiment", input, output);
593634
}
594635

636+
TEST_F(InsertGAFilterTest, AsynchronousDCCustomVarExperiment) {
637+
// dc.js version of AsynchronousGACustomVarExperiment.
638+
GoogleString experiment_string;
639+
SetUpCustomVarExperiment(false, &experiment_string);
640+
GoogleString input = StringPrintf(kAsyncDC, "", "", kGaId);
641+
GoogleString output = StringPrintf(kAsyncDC, "<head/>", StringPrintf(
642+
kGAExperimentSnippet, kGASpeedTracking, 4,
643+
experiment_string.c_str()).c_str(), kGaId);
644+
ValidateExpected("extend async dc.js for cv experiment", input, output);
645+
}
646+
595647
TEST_F(InsertGAFilterTest, AsynchronousGAContentExperiment) {
596648
// Show that we can add content experiment tracking to existing async ga.js
597649
// usage.

0 commit comments

Comments
 (0)