pagespeed experiment javascript code manually insertion ?

68 views
Skip to first unread message

Centmin Mod George

unread,
Jul 1, 2016, 10:23:26 PM7/1/16
to ngx-pagespeed-discuss
i setup pagespeed on/of experiment but i have custom dynamically inserted Google Analytics code via xenforo plugin which ngx_pagespeed doesn't seem to be able to append the following to:

_gaq.push(['_setCustomVar', 1, 'ExperimentState', 'Experiment: 1']);
_gaq.push(['_setCustomVar', 1, 'ExperimentState', 'Experiment: 2']);

i have

pagespeed RunExperiment on;
pagespeed UseAnalyticsJs off;
pagespeed AnalyticsID UA-XXXXXXX-Y;
pagespeed ExperimentVariable 1;
pagespeed EnableFilters insert_ga;
# ps on
pagespeed ExperimentSpec "id=1;percent=50;default";
# ps off
pagespeed ExperimentSpec "id=2;percent=50";

unable to edit xenforo plugin which inserts GA code as

<script type="text/javascript">_gaq=[["_setAccount","UA-XXXXXXX-Y"],["_setSiteSpeedSampleRate",100]];_gaq.push(["_trackPageview"]);(function(){var b=document.createElement("script");b.type="text/javascript";b.async=!0;b.src="//stats.g.doubleclick.net/dc.js";(document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0]).appendChild(b)})();
$(document).ready(function(){setTimeout(function(){try{FB.Event.subscribe("edge.create",function(a){_gaq.push(["_trackSocial","Facebook","Like",a])}),FB.Event.subscribe("edge.remove",function(a){_gaq.push(["_trackSocial","Facebook","Unlike",a])}),twttr.ready(function(a){a.events.bind("tweet",function(c){if(c){var a;c.target&&"IFRAME"==c.target.nodeName&&(a=extractParamFromUri(c.target.src,"url"));_gaq.push(["_trackSocial","Twitter","Tweet",a])}});a.events.bind("follow",function(a){if(a){var b;a.target&&
"IFRAME"==a.target.nodeName&&(b=extractParamFromUri(a.target.src,"url"));_gaq.push(["_trackSocial","Twitter","Follow",b])}})})}catch(b){}},1E3)});</script>

can i manually insert these in separate script call ?

_gaq.push(['_setCustomVar', 1, 'ExperimentState', 'Experiment: 1']);
_gaq.push(['_setCustomVar', 1, 'ExperimentState', 'Experiment: 2']);

if i can then i'd disable

pagespeed EnableFilters insert_ga;

right ?

thanks

George

Centmin Mod George

unread,
Jul 2, 2016, 11:29:13 AM7/2/16
to ngx-pagespeed-discuss

so setup id=0 which would default to normal pagespeed on ? and use id=3 for pagespeed off and manually just append

_gaq.push(['_setCustomVar', 1, 'ExperimentState', 'Experiment: 3']);

to my dynamically created GA code

with

pagespeed RunExperiment on;
pagespeed UseAnalyticsJs off;
pagespeed AnalyticsID UA-XXXXXXX-Y;
pagespeed ExperimentVariable 1;
#pagespeed EnableFilters insert_ga;
# ps off
pagespeed ExperimentSpec "id=3;percent=50";

Jeff Kaufman

unread,
Jul 6, 2016, 9:28:22 AM7/6/16
to ngx-pagesp...@googlegroups.com
For pagespeed to do experiment tracking, it needs to be able to insert
_gaq.push(['_setCustomVar', 1, 'ExperimentState', 'Experiment: 1']);
for one side of the experiment and _gaq.push(['_setCustomVar', 1,
'ExperimentState', 'Experiment: 2']); for the other side. Otherwise
you won't be able to tell them apart in Google Analytics.

PageSpeed currently doesn't recognize stats.g.doubleclick.net/dc.js as
a synonym for google-analytics.com/ga.js, but it should. I've filed a
bug https://github.com/pagespeed/mod_pagespeed/issues/1338 , and I
have a patch out for review.

Even with this fixed, though, it's still not going to work with your
script because yours starts with:

_gaq=[ ... ]

To be interoperable, gs.js snippet code should instead start with:

var _gaq = _gaq || [];

This means that whichever snippet comes first, settings from one won't
override the other. If you have:

script1: _gaq.push(["_setSiteSpeedSampleRate",100])
script2: _gaq=[["_setAccount","UA-XXXXXXX-Y"]]

Then you'll lose your _setSiteSpeedSampleRate, while if you have:

script1: var _gaq=_gaq || [];_gaq.push(["_setSiteSpeedSampleRate",100])
script2: var _gaq=_gaq || [];_gaq=[["_setAccount","UA-XXXXXXX-Y"]]

You'll get both settings into _gaq.

Can we file a bug with Xenforo to switch from

_gaq=[["_setAccount","UA-XXXXXXX-Y"],["_setSiteSpeedSampleRate",100]];

to

var _gaq=_gaq ||
[];_gaq.push(["_setAccount","UA-XXXXXXX-Y"]);_gaq.push(["_setSiteSpeedSampleRate",100]);

Is https://xenforo.com/community/forums/xenforo-questions-and-support.25/
the right place to do that?

Jeff
> --
> You received this message because you are subscribed to the Google Groups
> "ngx-pagespeed-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ngx-pagespeed-di...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ngx-pagespeed-discuss.
> For more options, visit https://groups.google.com/d/optout.

Centmin Mod George

unread,
Jul 6, 2016, 6:05:25 PM7/6/16
to ngx-pagespeed-discuss
Hi Jeff thanks for the insight and info. The actual GA code manipulation comes from a Xenforo addon called Better Analytics https://xenforo.com/community/resources/better-analytics.2288/ so i'll see about modifying it. The stats.g.doubleclick.net/dc,js is actually a synonym for ga.js ? didn't know that ! 

Jeff Kaufman

unread,
Jul 6, 2016, 6:07:50 PM7/6/16
to ngx-pagesp...@googlegroups.com
Well, it's not actually a synonym, but it is from ngx_pagespeed's
perspective? My understanding from reading docs (this morning) is
that Double Click's dc,js is an extension of ga.js that adds a few
more features.
Reply all
Reply to author
Forward
0 new messages