@@ -252,19 +252,24 @@ class InsertGAFilterTest : public RewriteTestBase {
252
252
}
253
253
254
254
void SetUpContentExperiment (bool use_analytics_js) {
255
+ SetUpContentExperiment (use_analytics_js, " 456" );
256
+ }
257
+
258
+ void SetUpContentExperiment (bool use_analytics_js,
259
+ const GoogleString& variant_id) {
255
260
NullMessageHandler handler;
256
261
RewriteOptions* options = rewrite_driver ()->options ()->Clone ();
257
262
options->set_use_analytics_js (use_analytics_js);
258
263
options->set_running_experiment (true );
259
- ASSERT_TRUE (options->AddExperimentSpec (
264
+ ASSERT_TRUE (options->AddExperimentSpec (StringPrintf (
260
265
" id=2;percent=10;slot=4;options="
261
266
" ContentExperimentID=123,"
262
- " ContentExperimentVariantID=456 " , &handler));
267
+ " ContentExperimentVariantID=%s " , variant_id. c_str ()) , &handler));
263
268
ASSERT_TRUE (options->AddExperimentSpec (
264
269
" id=7;percent=10;level=CoreFilters;slot=4;options="
265
270
" ContentExperimentID=123,"
266
271
" ContentExperimentVariantID=789" , &handler));
267
- options->SetExperimentState (2 ); // Expecting cxid=123, cxvid=456 .
272
+ options->SetExperimentState (2 ); // Expecting cxid=123, cxvid=variant_id .
268
273
269
274
// Setting up experiments automatically enables AddInstrumentation.
270
275
// Turn it off so our output is easier to understand.
@@ -376,7 +381,25 @@ TEST_F(InsertGAFilterTest, ExperimentGaJsCx) {
376
381
" \" ></script>" ).c_str (),
377
382
" " ,
378
383
StrCat (StringPrintf (kContentExperimentsSetChosenVariationSnippet ,
379
- " 456" , " 123" ),
384
+ 456 , " 123" ),
385
+ StringPrintf (kGAJsSnippet , kGaId , " test.com" ,
386
+ kGASpeedTracking )).c_str ());
387
+ ValidateExpected (" ga.js cx experiment" , kHtmlInput , output);
388
+ }
389
+
390
+ TEST_F (InsertGAFilterTest, ExperimentGaJsCxString) {
391
+ // Show that an attempt to insert a ga.js snippet with a string variant ID
392
+ // results in a warning message.
393
+ const GoogleString& kVariantText (" StringVariant" );
394
+ SetUpContentExperiment (false , kVariantText );
395
+ GoogleString output = StringPrintf (
396
+ kHtmlOutputFormat ,
397
+ StrCat (" <script src=\" " ,
398
+ kContentExperimentsJsClientUrl ,
399
+ " \" ></script>" ).c_str (),
400
+ " " ,
401
+ StrCat (StringPrintf (kContentExperimentsNonNumericVariantComment ,
402
+ kVariantText .c_str ()),
380
403
StringPrintf (kGAJsSnippet , kGaId , " test.com" ,
381
404
kGASpeedTracking )).c_str ());
382
405
ValidateExpected (" ga.js cx experiment" , kHtmlInput , output);
@@ -411,6 +434,21 @@ TEST_F(InsertGAFilterTest, ExperimentAnalyticsJsCx) {
411
434
ValidateExpected (" analytics.js cx experiment" , kHtmlInput , output);
412
435
}
413
436
437
+ TEST_F (InsertGAFilterTest, ExperimentAnalyticsJsCxString) {
438
+ // Show that we can insert an anlytics.js snippet that includes content
439
+ // experiment tracking where the variant is a string.
440
+ const GoogleString& kVariantText (" StringVariant" );
441
+ SetUpContentExperiment (true , kVariantText );
442
+ GoogleString output = StringPrintf (
443
+ kHtmlOutputFormat , " " , " " , StringPrintf (
444
+ kAnalyticsJsSnippet ,
445
+ kGaId ,
446
+ kAnalyticsJsIncreaseSiteSpeedTracking ,
447
+ StringPrintf (kContentExperimentsSetExpAndVariantSnippet ,
448
+ " 123" , kVariantText .c_str ()).c_str ()).c_str ());
449
+ ValidateExpected (" analytics.js cx experiment" , kHtmlInput , output);
450
+ }
451
+
414
452
const char kHtmlInputWithGASnippetFormat [] =
415
453
" <head>\n <title>Something</title>\n "
416
454
" </head><body> Hello World!"
@@ -529,7 +567,7 @@ TEST_F(InsertGAFilterTest, SynchronousGAContentExperiment) {
529
567
" \" ></script><script>" ,
530
568
StringPrintf (
531
569
kContentExperimentsSetChosenVariationSnippet ,
532
- " 456" , " 123" )).c_str (),
570
+ 456 , " 123" )).c_str (),
533
571
kGaId );
534
572
ValidateExpected (" extend sync ga.js for content experiment" , input, output);
535
573
}
@@ -568,7 +606,7 @@ TEST_F(InsertGAFilterTest, AsynchronousGAContentExperiment) {
568
606
" \" ></script><script>" ,
569
607
StringPrintf (
570
608
kContentExperimentsSetChosenVariationSnippet ,
571
- " 456" , " 123" )).c_str (),
609
+ 456 , " 123" )).c_str (),
572
610
kGaId );
573
611
ValidateExpected (" extend async ga.js for content experiment" , input, output);
574
612
}
@@ -764,7 +802,7 @@ TEST_F(InsertGAFilterTest, ExistingGaJsContentExperimentNoCloseAnything) {
764
802
"\"></script>"
765
803
"<script>",
766
804
StringPrintf(kContentExperimentsSetChosenVariationSnippet,
767
- " 456" , "123"),
805
+ 456, "123"),
768
806
StringPrintf(kGAJsSnippet, kGaId, "test.com",
769
807
kGASpeedTracking)).c_str());
770
808
@@ -786,7 +824,7 @@ TEST_F(InsertGAFilterTest, AsynchronousGAContentExperimentFlush) {
786
824
" \" ></script><script>" ,
787
825
StringPrintf (
788
826
kContentExperimentsSetChosenVariationSnippet ,
789
- " 456" , " 123" )).c_str (),
827
+ 456 , " 123" )).c_str (),
790
828
kGaId );
791
829
792
830
SetupWriter ();
0 commit comments