Permalink
Browse files

insert-ga: accept dc.js as a synonym for ga.js

Fixes #1338
  • Loading branch information...
jeffkaufman authored and crowell committed Jul 7, 2016
1 parent 68af83e commit 1ff43e196a7d0b1c9b27387ae0beef6e374cea51
Showing with 57 additions and 2 deletions.
  1. +5 −2 net/instaweb/rewriter/insert_ga_filter.cc
  2. +52 −0 net/instaweb/rewriter/insert_ga_filter_test.cc
@@ -221,8 +221,10 @@ void InsertGAFilter::StartElementImpl(HtmlElement* element) {
// some false positives; the later check is more thorough.
InsertGAFilter::AnalyticsStatus InsertGAFilter::FindSnippetInScript(
const GoogleString& s) {
// dc.js is a synonym for old-style ga.js
if (!seen_sync_ga_js_ &&
s.find("google-analytics.com/ga.js") != GoogleString::npos) {
(s.find("google-analytics.com/ga.js") != GoogleString::npos ||
s.find("stats.g.doubleclick.net/dc.js") != GoogleString::npos)) {
// The synchronous snippet has two parts: first one with
// [google-analytics.com/ga.js] (no initial dot) and then a later one with
// ga_id, _getTracker, and _trackPageview. Track that we've seen what is
@@ -236,7 +238,8 @@ InsertGAFilter::AnalyticsStatus InsertGAFilter::FindSnippetInScript(
}
if (s.find(".google-analytics.com/urchin.js") != GoogleString::npos) {
return kUnusableSnippetFound; // urchin.js is too old.
} else if (s.find(".google-analytics.com/ga.js") != GoogleString::npos) {
} else if (s.find(".google-analytics.com/ga.js") != GoogleString::npos ||
s.find("stats.g.doubleclick.net/dc.js") != GoogleString::npos) {
// With the async snippet there is one part that first loads ga.js
// (using [.google-analytics.com/ga.js], with initial dot) and then has the
// ga_id (which we checked for above).
@@ -99,6 +99,36 @@ const char kAsyncGA[] =
"})();"
"</script>";

const char kSynchronousDC[] =
"%s"
"<script>"
" var gaJsHost = ((\"https:\" == document.location.protocol) ?"
" \"https://\" : \"http://\");"
" document.write(unescape(\"%%3Cscript src='\" + gaJsHost +"
" \"stats.g.doubleclick.net/dc.js'"
" type='text/javascript'%%3E%%3C/script%%3E\"));"
"</script>"
"<script>%s"
" try { var pageTracker = _gat._getTracker(\"%s\");"
" pageTracker._trackPageview(); } catch(err) {}"
"</script>";

const char kAsyncDC[] =
"%s"
"<script type='text/javascript'>document.write('another script');</script>"
"<script>%s"
"var _gaq = _gaq || [];"
"_gaq.push(['_setAccount', '%s']);"
"_gaq.push(['_trackPageview']);"
"(function() {"
" var ga = document.createElement('script');"
" ga.src = ('https:' == document.location.protocol ?"
" 'https://' : 'http://') +"
" 'stats.g.doubleclick.net/dc.js'';"
" ga.setAttribute('async', 'true');"
" document.documentElement.firstChild.appendChild(ga);"
"})();"
"</script>";

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

TEST_F(InsertGAFilterTest, SynchronousDCCustomVarExperiment) {
// dc.js version of SynchronousGACustomVarExperiment.
GoogleString experiment_string;
SetUpCustomVarExperiment(false, &experiment_string);
GoogleString input = StringPrintf(kSynchronousDC, "", "", kGaId);
GoogleString output = StringPrintf(kSynchronousDC, "<head/>", StringPrintf(
kGAExperimentSnippet, kGASpeedTracking, 4,
experiment_string.c_str()).c_str(), kGaId);
ValidateExpected("extend sync dc.js for cv experiment", input, output);
}

TEST_F(InsertGAFilterTest, SynchronousGAContentExperiment) {
// Show that we can add content experiment tracking to existing synchronous
// ga.js usage.
@@ -592,6 +633,17 @@ TEST_F(InsertGAFilterTest, AsynchronousGACustomVarExperiment) {
ValidateExpected("extend async ga.js for cv experiment", input, output);
}

TEST_F(InsertGAFilterTest, AsynchronousDCCustomVarExperiment) {
// dc.js version of AsynchronousGACustomVarExperiment.
GoogleString experiment_string;
SetUpCustomVarExperiment(false, &experiment_string);
GoogleString input = StringPrintf(kAsyncDC, "", "", kGaId);
GoogleString output = StringPrintf(kAsyncDC, "<head/>", StringPrintf(
kGAExperimentSnippet, kGASpeedTracking, 4,
experiment_string.c_str()).c_str(), kGaId);
ValidateExpected("extend async dc.js for cv experiment", input, output);
}

TEST_F(InsertGAFilterTest, AsynchronousGAContentExperiment) {
// Show that we can add content experiment tracking to existing async ga.js
// usage.

0 comments on commit 1ff43e1

Please sign in to comment.