fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-08-03 04:22 am

[dw-free] Update inserted Google Analytics snippet to latest version

[commit: http://hg.dwscoalition.org/dw-free/rev/ba3d26cc4acb]

http://bugs.dwscoalition.org/show_bug.cgi?id=3765

Use async: faster, more accurate. Splits the code to be rendered into
head/body.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/DW/PageStats/GoogleAnalytics.pm
  • cgi-bin/DW/Template/Plugin/SiteScheme.pm
  • cgi-bin/LJ/PageStats.pm
  • cgi-bin/LJ/S2.pm
  • cgi-bin/weblib.pl
  • schemes/common.tt
--------------------------------------------------------------------------------
diff -r b7a2c77e9360 -r ba3d26cc4acb cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Wed Aug 03 11:35:53 2011 +0800
+++ b/cgi-bin/Apache/LiveJournal.pm	Wed Aug 03 12:21:52 2011 +0800
@@ -1492,7 +1492,8 @@
         $cb->(\$html);
     }
 
-    # add crap before </body>
+
+    # add stuff before </body>
     my $before_body_close = "";
     LJ::Hooks::run_hooks("insert_html_before_body_close", \$before_body_close);
     LJ::Hooks::run_hooks("insert_html_before_journalctx_body_close", \$before_body_close);
diff -r b7a2c77e9360 -r ba3d26cc4acb cgi-bin/DW/PageStats/GoogleAnalytics.pm
--- a/cgi-bin/DW/PageStats/GoogleAnalytics.pm	Wed Aug 03 11:35:53 2011 +0800
+++ b/cgi-bin/DW/PageStats/GoogleAnalytics.pm	Wed Aug 03 12:21:52 2011 +0800
@@ -17,9 +17,8 @@
 use base 'LJ::PageStats';
 use strict;
 
-sub render {
+sub _render_head {
     my ( $self ) = @_;
-
     return '' unless $self->should_do_pagestats;
 
     my $ctx = $self->get_context;
@@ -34,16 +33,26 @@
         # validation was implemented.
         $code = LJ::ejs( $code );
     }
-    
-    return qq{
-<script type="text/javascript">
-var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
-document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
-</script>
-<script type="text/javascript">
-var pageTracker = _gat._getTracker("$code");
-pageTracker._initData();
-pageTracker._trackPageview();
+
+    return qq{<script type="text/javascript">
+  var _gaq = _gaq || [];
+  _gaq.push(['_setAccount', '$code']);
+  _gaq.push(['_trackPageview']);</script>
+};
+}
+
+sub _render {
+    my ( $self ) = @_;
+
+    return '' unless $self->should_do_pagestats;
+
+    return qq{<script type="text/javascript">
+  (function() {
+    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+  })();
+
 </script>
 };
 }
diff -r b7a2c77e9360 -r ba3d26cc4acb cgi-bin/DW/Template/Plugin/SiteScheme.pm
--- a/cgi-bin/DW/Template/Plugin/SiteScheme.pm	Wed Aug 03 11:35:53 2011 +0800
+++ b/cgi-bin/DW/Template/Plugin/SiteScheme.pm	Wed Aug 03 12:21:52 2011 +0800
@@ -61,6 +61,10 @@
     return LJ::res_includes();
 }
 
+sub final_head_html {
+    return LJ::final_head_html();
+}
+
 sub final_body_html {
     return LJ::final_body_html();
 }
diff -r b7a2c77e9360 -r ba3d26cc4acb cgi-bin/LJ/PageStats.pm
--- a/cgi-bin/LJ/PageStats.pm	Wed Aug 03 11:35:53 2011 +0800
+++ b/cgi-bin/LJ/PageStats.pm	Wed Aug 03 12:21:52 2011 +0800
@@ -51,13 +51,41 @@
         die "Error loading PageStats '$plugin': $@" if $@;
         my $plugin_obj = $class->new;
         next unless $plugin_obj->should_render;
-        $output .= $plugin_obj->render(conf => $self->{conf}->{$plugin});
+        $output .= $plugin_obj->_render(conf => $self->{conf}->{$plugin});
     }
 
     # return nothing
     return "<div id='hello-world' style='text-align: left; font-size:0; line-height:0; height:0; overflow:hidden;'>$output</div>";
 }
 
+# render JS output that goes into the <head> tags
+sub render_head {
+    my ( $self ) = @_;
+    my $ctx = $self->get_context;
+
+    return '' unless $self->should_do_pagestats;
+
+    my $output = '';
+    foreach my $plugin ( $self->get_active_plugins ) {
+        my $class = $plugin;
+        eval "use $class; 1;";
+        die "Error loading PageStats '$plugin': $@" if $@;
+        my $plugin_obj = $class->new;
+        next unless $plugin_obj->should_render;
+        $output .= $plugin_obj->_render_head( conf => $self->{conf}->{$plugin} );
+    }
+
+    return $output;
+}
+
+sub _render {
+    return "";
+}
+
+sub _render_head {
+    return "";
+}
+
 # method on root object (LJ::PageStats instance) to decide if user has
 # opted-out of page stats tracking.  Note: this isn't pagestat-specific logic.
 #  that's in the "should_render" method.
diff -r b7a2c77e9360 -r ba3d26cc4acb cgi-bin/LJ/S2.pm
--- a/cgi-bin/LJ/S2.pm	Wed Aug 03 11:35:53 2011 +0800
+++ b/cgi-bin/LJ/S2.pm	Wed Aug 03 12:21:52 2011 +0800
@@ -208,6 +208,7 @@
         unless $ctx->[S2::SCRATCH]->{siteviews_enabled};
 
     $page->{head_content} .= $extra_js;
+    $page->{head_content} .= LJ::pagestats_obj()->render_head( 'journal' );
 
     # inject the control strip JS, but only after any libraries have been injected
     $page->{head_content} .= LJ::control_strip_js_inject( user => $u->user, jquery => $beta_jquery )
diff -r b7a2c77e9360 -r ba3d26cc4acb cgi-bin/weblib.pl
--- a/cgi-bin/weblib.pl	Wed Aug 03 11:35:53 2011 +0800
+++ b/cgi-bin/weblib.pl	Wed Aug 03 12:21:52 2011 +0800
@@ -3833,6 +3833,16 @@
 </script>
 };
 
+sub final_head_html {
+    my $ret = "";
+
+    my $pagestats_obj = LJ::pagestats_obj();
+    $ret .= $pagestats_obj->render_head
+        if $pagestats_obj;
+
+    return $ret;
+}
+
 # returns HTML which should appear before </body>
 sub final_body_html {
     my $before_body_close = "";
diff -r b7a2c77e9360 -r ba3d26cc4acb schemes/common.tt
--- a/schemes/common.tt	Wed Aug 03 11:35:53 2011 +0800
+++ b/schemes/common.tt	Wed Aug 03 12:21:52 2011 +0800
@@ -30,6 +30,7 @@
     [%- PROCESS block.need_res -%]
     [% dw_scheme.res_includes %]
     [% sections.head %]
+    [% dw_scheme.final_head_html %]
 </head>
 [%- END -%]
 
--------------------------------------------------------------------------------