diff --git a/controller/Session.class.php b/controller/Session.class.php index 0da141e6..5387dd2e 100644 --- a/controller/Session.class.php +++ b/controller/Session.class.php @@ -11,13 +11,16 @@ class Session const NAMESPACE_DEFAULT = 'default', NAMESPACE_FLASH = 'flash', - NAMESPACE_FLASH_REMOVE = 'flash_remove'; + NAMESPACE_FLASH_REMOVE = 'flash_remove', + USER_ID = 'user_id', + SITE_ID = 'lbry.io'; public static function init() { ini_set('session.cookie_secure', IS_PRODUCTION); // send cookie over ssl only - ini_set('session.cookie_httponly', true); // no js access to cookies - session_start(); + ini_set('session.cookie_httponly', true); // no js access to cookies + session_start(); + if (!static::get('secure_and_httponly_set')) { session_regenerate_id(); // ensure that old cookies get new settings @@ -31,6 +34,18 @@ class Session static::setNamespace(static::NAMESPACE_DEFAULT, $oldSession); } + Response::addPostRenderCallback(function(){ + $site_visitor_id = key_exists(static::USER_ID, $_SESSION) ? $_SESSION[static::USER_ID] : ''; + $response = LBRY::logWebVisitor(static::SITE_ID, $site_visitor_id, $_SERVER['REMOTE_ADDR']); + if (!is_null($response) + && key_exists('data', $response) + && key_exists('visitor_id', $response['data'])) { + $_SESSION[static::USER_ID] = $response['data']['visitor_id']; + } else { + $_SESSION[static::USER_ID] = ''; + } + }); + static::initFlashes(); } diff --git a/lib/thirdparty/LBRY.class.php b/lib/thirdparty/LBRY.class.php index a6223d24..3a80394d 100644 --- a/lib/thirdparty/LBRY.class.php +++ b/lib/thirdparty/LBRY.class.php @@ -75,4 +75,12 @@ class LBRY return Curl::post(static::getApiUrl("/yt/update"), ['status_token' => $status_token, 'new_email' => $email, 'new_preferred_channel' => $channel_name, 'sync_consent' => $sync_consent], ['json_response' => true]); } } + + public static function logWebVisitor($site, $visitorID, $IPAddress) + { + if (IS_PRODUCTION) + { + return Curl::post(static::getApiUrl("/visitor/new"), ['site' => $site, 'visitor_id' => $visitorID, 'ip_address' => $IPAddress], ['json_response' => true]); + } + } } diff --git a/view/Response.class.php b/view/Response.class.php index 9b4526ba..f93e771b 100644 --- a/view/Response.class.php +++ b/view/Response.class.php @@ -35,6 +35,8 @@ class Response protected static $metaImages = []; protected static $facebookAnalyticsType = "PageView"; + private static $PostRenderCallbacks = array(); + public static function setMetaDescription($description) { static::$metaDescription = $description; @@ -369,6 +371,19 @@ class Response ); } + public static function addPostRenderCallback( $cb ) + { + array_push(static::$PostRenderCallbacks, $cb); + } + + public static function invokePostRenderCallbacks() + { + foreach(static::$PostRenderCallbacks as &$cb ) + { + $cb(); + } + } + // public static function addBodyCssClass($classOrClasses) // { diff --git a/view/template/layout/_analytics_footer.php b/view/template/layout/_analytics_footer.php index d5432e90..1618d232 100644 --- a/view/template/layout/_analytics_footer.php +++ b/view/template/layout/_analytics_footer.php @@ -8,11 +8,12 @@ ga('create', 'UA-60403362-1', 'auto'); ga('require', 'GTM-NT8579P'); + ga('set', 'userId', ''); ga('send', 'pageview'); !function(f,b,e,v,n,t,s) {if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)}; if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t,s)}(window,document,'script', 'https://connect.facebook.net/en_US/fbevents.js'); - fbq('init', '1618717031725766'); + fbq('init', '1618717031725766',{uid: ''})); fbq('track', ''); diff --git a/web/index.php b/web/index.php index 4f06d8a7..2864c89c 100644 --- a/web/index.php +++ b/web/index.php @@ -28,6 +28,8 @@ try { View::compileCss(); } Controller::dispatch(Request::getRoutingUri()); + flush(); + Response::invokePostRenderCallbacks(); } catch (Throwable $e) { if (IS_PRODUCTION) { Slack::sendErrorIfProd($e);