pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)
Res facta quae tamen fingi potuit ([personal profile] pauamma) wrote in [site community profile] changelog2009-03-30 05:11 pm

[dw-free] Google Analytics integration

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

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

Google Analytics integration.

Patch by [personal profile] exor674.

Files modified:
  • bin/upgrading/en.dat
  • bin/upgrading/proplists.dat
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/DW/PageStats/GoogleAnalytics.pm
  • cgi-bin/DW/Setting/GoogleAnalytics.pm
  • cgi-bin/LJ/PageStats.pm
  • cgi-bin/LJ/User.pm
  • doc/config-private.pl.txt
  • etc/config.pl
  • htdocs/manage/settings/index.bml
--------------------------------------------------------------------------------
diff -r 7b7866228850 -r 460ba10f3baf bin/upgrading/en.dat
--- a/bin/upgrading/en.dat	Sun Mar 29 23:49:09 2009 +0000
+++ b/bin/upgrading/en.dat	Mon Mar 30 17:09:45 2009 +0000
@@ -2683,6 +2683,8 @@ setting.graphicpreviews.option.comm=Allo
 
 setting.graphicpreviews.option.self=Display a preview of external links when hovering over them
 
+setting.googleanalytics.label=Google Analytics ID
+
 setting.imageplaceholders.error.invalid=Invalid image size.
 
 setting.imageplaceholders.label=Image Placeholders
diff -r 7b7866228850 -r 460ba10f3baf bin/upgrading/proplists.dat
--- a/bin/upgrading/proplists.dat	Sun Mar 29 23:49:09 2009 +0000
+++ b/bin/upgrading/proplists.dat	Mon Mar 30 17:09:45 2009 +0000
@@ -325,6 +325,14 @@ userproplist.google_talk:
   indexed: 1
   multihomed: 1
   prettyname: Google Talk Address
+
+userproplist.google_analytics:
+  cldversion: 4
+  datatype: char
+  des: Google Analytics Code
+  indexed: 1
+  multihomed: 0
+  prettyname: Google Analytics Code
 
 userproplist.hide_adult_content:
   cldversion: 4
diff -r 7b7866228850 -r 460ba10f3baf cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Sun Mar 29 23:49:09 2009 +0000
+++ b/cgi-bin/Apache/LiveJournal.pm	Mon Mar 30 17:09:45 2009 +0000
@@ -487,6 +487,7 @@ sub trans
                     $cookie = 1 if $BML::COOKIE{LJ::ContentFlag->cookie_name("explicit")};
                 }
 
+                LJ::set_active_journal( $u );
                 $r->pnotes->{'user'} = $u;
                 $r->pnotes->{'entry'} = $entry if $entry;
                 $r->notes->{'returl'} = $returl;
diff -r 7b7866228850 -r 460ba10f3baf cgi-bin/DW/PageStats/GoogleAnalytics.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/PageStats/GoogleAnalytics.pm	Mon Mar 30 17:09:45 2009 +0000
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+#
+# DW::PageStats::GoogleAnalytics
+#
+# LJ::PageStats module for Google Analytics
+#
+# Authors:
+#      Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2009 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself.  For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+package DW::PageStats::GoogleAnalytics;
+use base 'LJ::PageStats';
+use strict;
+
+sub render {
+    my ( $self ) = @_;
+
+    return '' unless $self->should_do_pagestats;
+
+    my $ctx = $self->get_context;
+
+    my $code;
+    if ( $ctx eq 'app' ) {
+        $code = $LJ::SITE_PAGESTAT_CONFIG{google_analytics};
+    } elsif ( $ctx eq 'journal' ) {
+        $code = LJ::get_active_journal()->google_analytics;
+    }
+
+    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();
+</script>
+};
+}
+
+sub should_render {
+    my ( $self ) = @_;
+
+    my $ctx = $self->get_context;
+    return 0 unless $ctx && $ctx =~ /^(app|journal)$/;
+
+    if ( $ctx eq 'app' ) {
+        return 1 if defined $LJ::SITE_PAGESTAT_CONFIG{google_analytics};
+    } elsif ( $ctx eq 'journal' ) {
+        my $u = LJ::get_active_journal();
+        return $u && $u->can_use_google_analytics && $u->google_analytics ? 1 : 0;
+    }
+    return 0;
+}
+
+1;
diff -r 7b7866228850 -r 460ba10f3baf cgi-bin/DW/Setting/GoogleAnalytics.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Setting/GoogleAnalytics.pm	Mon Mar 30 17:09:45 2009 +0000
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+#
+# DW::Setting::GoogleAnalytics
+#
+# LJ::Setting module for Google Analytics
+#
+# Authors:
+#      Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2009 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself.  For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+package DW::Setting::GoogleAnalytics;
+use base 'LJ::Setting';
+use strict;
+use warnings;
+use LJ::Constants;
+
+sub should_render {
+    my ( $class, $u ) = @_;
+
+    return $u && $u->can_use_google_analytics ? 1 : 0;
+}
+
+sub label {
+    return $_[0]->ml( 'setting.googleanalytics.label' );
+}
+
+sub option {
+    my ( $class, $u, $errs, $args ) = @_;
+
+    my $key = $class->pkgkey;
+    my $ret;
+
+    $ret .= LJ::html_text({
+        name  => "${key}code",
+        id    => "${key}code",
+        class => "text",
+        value => $errs ? $class->get_arg( $args, "code" ) : $u->google_analytics,
+        size  => 30,
+        maxlength => 100,
+    });
+
+    my $errdiv = $class->errdiv( $errs, "code" );
+    $ret .= "<br />$errdiv" if $errdiv;
+
+    return $ret;
+}
+
+sub save {
+    my ( $class, $u, $args ) = @_;
+
+    my $txt = $class->get_arg( $args, "code" );
+    $txt = LJ::trim( $txt || "" );
+    $txt = LJ::text_trim( $txt, 0, 100 );
+    $u->google_analytics ( $txt );
+    return 1;
+}
+
+1;
diff -r 7b7866228850 -r 460ba10f3baf cgi-bin/LJ/PageStats.pm
--- a/cgi-bin/LJ/PageStats.pm	Sun Mar 29 23:49:09 2009 +0000
+++ b/cgi-bin/LJ/PageStats.pm	Mon Mar 30 17:09:45 2009 +0000
@@ -25,7 +25,7 @@ sub render {
 
     my $output = '';
     foreach my $plugin ($self->get_active_plugins) {
-        my $class = "LJ::PageStats::$plugin";
+        my $class = $plugin;
         eval "use $class; 1;";
         die "Error loading PageStats '$plugin': $@" if $@;
         my $plugin_obj = $class->new;
@@ -222,7 +222,7 @@ sub campaign_tracking {
 
     my $output = '';
     foreach my $plugin ($self->get_active_plugins) {
-        my $class = "LJ::PageStats::$plugin";
+        my $class = $plugin;
         eval "use $class; 1;";
         die "Error loading PageStats '$plugin': $@" if $@;
         my $plugin_obj = $class->new;
diff -r 7b7866228850 -r 460ba10f3baf cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Sun Mar 29 23:49:09 2009 +0000
+++ b/cgi-bin/LJ/User.pm	Mon Mar 30 17:09:45 2009 +0000
@@ -1737,6 +1737,11 @@ sub can_show_onlinestatus {
 }
 
 
+sub can_use_google_analytics {
+    return $_[0]->get_cap( 'google_analytics' ) ? 1 : 0;
+}
+
+
 # <LJFUNC>
 # name: LJ::User::caps_icon
 # des: get the icon for a user's cap.
@@ -1827,6 +1832,19 @@ sub gizmo_account_validated {
     }
 
     return $validated;
+}
+
+
+# get/set the Google Analytics ID
+sub google_analytics {
+    my $u = shift;
+
+    if ( defined $_[0] ) {
+        $u->set_prop( google_analytics => $_[0] );
+        return $_[0];
+    }
+
+    return $u->prop( 'google_analytics' );
 }
 
 
@@ -2111,7 +2129,7 @@ sub should_show_schools_to {
 # should show the thread expander for this user/journal
 sub show_thread_expander {
     my ( $u, $remote ) = @_;
-    
+
     return 1 if $remote && $remote->get_cap( 'thread_expander' )
         || $u->get_cap( 'thread_expander' );
 
diff -r 7b7866228850 -r 460ba10f3baf doc/config-private.pl.txt
--- a/doc/config-private.pl.txt	Sun Mar 29 23:49:09 2009 +0000
+++ b/doc/config-private.pl.txt	Mon Mar 30 17:09:45 2009 +0000
@@ -27,12 +27,16 @@
     # not logged in, this username will be used instead. (You should own this
     # account so that nobody can take it.)
     $EXAMPLE_USER_ACCOUNT = "username";
-    
+
     # list of official journals, as a list of "'username' => 1" pairs
     # used to determine whether to fire off an OfficialPost notification
     # when an entry is posted; hash instead of array for efficiency
     %OFFICIAL_JOURNALS = (
         news => 1,
+    );
+
+    %SITE_PAGESTAT_CONFIG = (
+    #    google_analytics => ,
     );
 }
 
diff -r 7b7866228850 -r 460ba10f3baf etc/config.pl
--- a/etc/config.pl	Sun Mar 29 23:49:09 2009 +0000
+++ b/etc/config.pl	Mon Mar 30 17:09:45 2009 +0000
@@ -364,6 +364,7 @@
             'friendsviewupdate' => 0,
             'full_rss' => 1,
             'getselfemail' => 0,
+            google_analytics => 0,
             'inbox_max' => 2000,
             'makepoll' => 0,
             'mass_privacy' => 0,
@@ -415,6 +416,7 @@
             'friendsviewupdate' => 1,
             'full_rss' => 1,
             'getselfemail' => 1,
+            google_analytics => 1,
             'inbox_max' => 4000,
             'makepoll' => 1,
             'mass_privacy' => 1,
@@ -464,6 +466,7 @@
             'friendsviewupdate' => 1,
             'full_rss' => 1,
             'getselfemail' => 1,
+            google_analytics => 1,
             'inbox_max' => 6000,
             'makepoll' => 1,
             'mass_privacy' => 1,
@@ -520,6 +523,7 @@
             'friendsviewupdate' => 1,
             'full_rss' => 1,
             'getselfemail' => 1,
+            google_analytics => 1,
             'inbox_max' => 6000,
             'makepoll' => 1,
             'mass_privacy' => 1,
@@ -569,6 +573,7 @@
             'friendsviewupdate' => 1,
             'full_rss' => 1,
             'getselfemail' => 1,
+            google_analytics => 1,
             'inbox_max' => 6000,
             'makepoll' => 1,
             'mass_privacy' => 1,
@@ -820,6 +825,13 @@
         opt_mangleemail => 'Y',
         moodthemeid => 7,
     );
+
+    # Stats
+    %PAGESTATS_PLUGIN_CONF = (
+        '_active' => [
+            'DW::PageStats::GoogleAnalytics',
+        ],
+    );
 }
 
 1;
diff -r 7b7866228850 -r 460ba10f3baf htdocs/manage/settings/index.bml
--- a/htdocs/manage/settings/index.bml	Sun Mar 29 23:49:09 2009 +0000
+++ b/htdocs/manage/settings/index.bml	Mon Mar 30 17:09:45 2009 +0000
@@ -69,6 +69,7 @@ body<=
                 LJ::Setting::ViewingAdultContent
                 LJ::Setting::SafeSearch
                 LJ::Setting::CyrillicServices
+                DW::Setting::GoogleAnalytics
             )],
         },
         notifications => {
--------------------------------------------------------------------------------