From a896be22a356f5ee4d9c15cd92783ad154b15fe6 Mon Sep 17 00:00:00 2001 From: Leigh B Stoller Date: Wed, 19 Apr 2017 08:36:45 -0600 Subject: [PATCH] Add support for user session tracking and Google Analytics. If we assign a unique constant random value to every user, and spit that out in the initial ga() stuff at the beginning of each page load, google can combine interactions from the same user across different devices. Say, like work vs home computer. --- db/User.pm.in | 5 +++++ sql/database-create.sql | 1 + sql/updates/4/575 | 17 +++++++++++++++++ www/aptui/quickvm_sup.php | 13 +++++++++---- www/tbauth.php3 | 14 ++++++++++++-- www/user_defs.php | 11 ++++++++++- 6 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 sql/updates/4/575 diff --git a/db/User.pm.in b/db/User.pm.in index c9545f7ac..d00a7f7b2 100644 --- a/db/User.pm.in +++ b/db/User.pm.in @@ -225,6 +225,7 @@ sub nonlocal_type($) { return field($_[0], "nonlocal_type"); } sub IsLocal($) { return (defined($_[0]->nonlocal_id()) ? 0 : 1); }; sub IsNonLocal($) { return (defined($_[0]->nonlocal_id()) ? 1 : 0); }; sub portal($) { return field($_[0], "portal"); } +sub ga_userid($) { return field($_[0], "ga_userid"); } sub Brand($) { return $_[0]->{'BRAND'}; } sub isAPT($) { return $_[0]->Brand()->isAPT() ? 1 : 0; } sub isCloud($) { return $_[0]->Brand()->isCloud() ? 1 : 0; } @@ -614,6 +615,9 @@ sub Create($$$$) # And a verification key. my $verify_key = TBGenSecretKey(); + # Google Analytics user id. + my $ga_userid = substr(TBGenSecretKey(), 0, 32); + if (! $nouuid) { # And a UUID (universally unique identifier). $uuid = NewUUID(); @@ -637,6 +641,7 @@ sub Create($$$$) push(@insert_data, "verify_key='$verify_key'"); push(@insert_data, "uid_idx='$uid_idx'"); push(@insert_data, "uid='$uid'"); + push(@insert_data, "ga_userid='$ga_userid'"); if ($archived) { # diff --git a/sql/database-create.sql b/sql/database-create.sql index 61d01b534..4c4c2f818 100644 --- a/sql/database-create.sql +++ b/sql/database-create.sql @@ -5156,6 +5156,7 @@ CREATE TABLE `users` ( `initial_passphrase` varchar(128) default NULL, `genesis` enum('emulab','aptlab','cloudlab','phantomnet') NOT NULL default 'emulab', `portal` enum('emulab','aptlab','cloudlab','phantomnet') default NULL, + `ga_userid` varchar(32) default NULL, PRIMARY KEY (`uid_idx`), KEY `unix_uid` (`unix_uid`), KEY `status` (`status`), diff --git a/sql/updates/4/575 b/sql/updates/4/575 new file mode 100644 index 000000000..047009615 --- /dev/null +++ b/sql/updates/4/575 @@ -0,0 +1,17 @@ +use strict; +use libdb; + +sub DoUpdate($$$) +{ + my ($dbhandle, $dbname, $version) = @_; + + if (!DBSlotExists("users", "ga_userid")) { + DBQueryFatal("alter table users add " . + " `ga_userid` varchar(32) default NULL after portal"); + } + return 0; +} + +# Local Variables: +# mode:perl +# End: diff --git a/www/aptui/quickvm_sup.php b/www/aptui/quickvm_sup.php index 271a8f561..b4ef17355 100644 --- a/www/aptui/quickvm_sup.php +++ b/www/aptui/quickvm_sup.php @@ -99,6 +99,7 @@ $PAGEHEADER_FUNCTION = function($thinheader = 0, $ignore1 = NULL, if (($login_user = CheckLogin($status)) != null) { $login_status = $status; $login_uid = $login_user->uid(); + $ga_userid = $login_user->ga_userid(); } if ($login_user && !($login_status & CHECKLOGIN_WEBONLY)) { $showmenus = 1; @@ -140,10 +141,14 @@ $PAGEHEADER_FUNCTION = function($thinheader = 0, $ignore1 = NULL, if ($TBMAINSITE && !$embedded && file_exists("../google-analytics.php")) { readfile("../google-analytics.php"); - echo ""; + echo ""; } echo " diff --git a/www/tbauth.php3 b/www/tbauth.php3 index 0be75decd..32c9bb1a4 100644 --- a/www/tbauth.php3 +++ b/www/tbauth.php3 @@ -1,6 +1,6 @@ ga_userid(); + if (!$ga_userid) { + $ga_userid = substr(GENHASH(), 0, 32); + $CHECKLOGIN_USER->SetGaUserid($ga_userid); + } + # # Now add in the modifiers. # @@ -884,6 +889,7 @@ function DOLOGIN($token, $password, $adminmode = 0, $nopassword = 0) { $usr_name = $user->name(); $uid_idx = $user->uid_idx(); $usr_email = $user->email(); + $ga_userid = $user->ga_userid(); # Check for frozen accounts. We do not update the IP record when # an account is frozen. @@ -918,6 +924,10 @@ function DOLOGIN($token, $password, $adminmode = 0, $nopassword = 0) { # # Pass! # + if (!$ga_userid) { + $ga_userid = substr(GENHASH(), 0, 32); + $user->SetGaUserid($ga_userid); + } # But inactive users need special handling. if ($user->status() == TBDB_USERSTATUS_INACTIVE) { diff --git a/www/user_defs.php b/www/user_defs.php index ea8100b23..d7e9506b7 100644 --- a/www/user_defs.php +++ b/www/user_defs.php @@ -1,6 +1,6 @@ field("mailman_password"); } function nonlocal_id() { return $this->field("nonlocal_id"); } function portal() { return $this->field("portal"); } + function ga_userid() { return $this->field("ga_userid"); } function isAPT() { return ($this->portal() && $this->portal() == "aptlab" ? 1 : 0); } function isCloud() { return ($this->portal() && @@ -1199,6 +1200,14 @@ class User return mysql_num_rows($query_result); } + function SetGaUserid($id) { + $idx = $this->uid_idx(); + + DBQueryFatal("update users set ga_userid='$id' ". + "where uid_idx='$idx'"); + $this->user["ga_userid"] = $id; + return 0; + } # # Return project access list for a user. This returns just pid,eid for -- GitLab