Merge pull request #886 from lbryio/web_visitor_logging

Set unique user id for google analytics and facebook pixel.
This commit is contained in:
Mark 2018-12-31 15:49:13 -05:00 committed by GitHub
commit 95bc7247f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 4 deletions

View file

@ -11,7 +11,9 @@ class Session
const NAMESPACE_DEFAULT = 'default', const NAMESPACE_DEFAULT = 'default',
NAMESPACE_FLASH = 'flash', 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() public static function init()
{ {
@ -19,6 +21,7 @@ class Session
ini_set('session.cookie_httponly', true); // no js access to cookies ini_set('session.cookie_httponly', true); // no js access to cookies
session_start(); session_start();
if (!static::get('secure_and_httponly_set')) { if (!static::get('secure_and_httponly_set')) {
session_regenerate_id(); // ensure that old cookies get new settings session_regenerate_id(); // ensure that old cookies get new settings
} }
@ -31,6 +34,18 @@ class Session
static::setNamespace(static::NAMESPACE_DEFAULT, $oldSession); 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(); static::initFlashes();
} }

View file

@ -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]); 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]);
}
}
} }

View file

@ -35,6 +35,8 @@ class Response
protected static $metaImages = []; protected static $metaImages = [];
protected static $facebookAnalyticsType = "PageView"; protected static $facebookAnalyticsType = "PageView";
private static $PostRenderCallbacks = array();
public static function setMetaDescription($description) public static function setMetaDescription($description)
{ {
static::$metaDescription = $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) // public static function addBodyCssClass($classOrClasses)
// { // {

View file

@ -8,11 +8,12 @@
ga('create', 'UA-60403362-1', 'auto'); ga('create', 'UA-60403362-1', 'auto');
ga('require', 'GTM-NT8579P'); ga('require', 'GTM-NT8579P');
ga('set', 'userId', '<?php echo $_SESSION[Session::USER_ID] ?>');
ga('send', 'pageview'); 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'); !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: '<?php echo $_SESSION[Session::USER_ID] ?>'}));
fbq('track', '<?php echo Response::getFacebookPixelAnalyticsType() ?>'); fbq('track', '<?php echo Response::getFacebookPixelAnalyticsType() ?>');
</script> </script>

View file

@ -28,6 +28,8 @@ try {
View::compileCss(); View::compileCss();
} }
Controller::dispatch(Request::getRoutingUri()); Controller::dispatch(Request::getRoutingUri());
flush();
Response::invokePostRenderCallbacks();
} catch (Throwable $e) { } catch (Throwable $e) {
if (IS_PRODUCTION) { if (IS_PRODUCTION) {
Slack::sendErrorIfProd($e); Slack::sendErrorIfProd($e);