Files
thetool/Layout/default/includes/openreplay.php
2026-02-02 10:14:38 +01:00

80 lines
2.8 KiB
PHP

<?php
/**
* OpenReplay Session Recording Integration
* Include this file in header templates to enable session recording.
*
* Usage: <?php include(__DIR__ . "/includes/openreplay.php"); ?>
*
* Variables that can be set before including:
* - $openreplayUserType: 'internal' (default) or 'external'
* - $openreplayDisabled: set to true to disable tracking
*/
if (!empty($openreplayDisabled)) return;
$openreplayUserId = '';
$openreplayUserName = '';
$openreplayUserType = $openreplayUserType ?? 'internal';
$openreplayMetadata = [];
if (class_exists('mfUser') && class_exists('mfLoginController') && mfLoginController::isLoggedIn()) {
$user = mfUser::singleton();
if ($user && $user->id) {
$openreplayUserId = !empty($user->email) ? $user->email : (string) $user->id;
$openreplayUserName = $user->username ?? '';
$openreplayMetadata['userType'] = $openreplayUserType;
$openreplayMetadata['username'] = $openreplayUserName;
$openreplayMetadata['workerId'] = (string) $user->id;
}
}
// Allow override from JSGlobals (for PWA contexts)
if (isset($JSGlobals['OPENREPLAY_USER_ID'])) {
$openreplayUserId = (string) $JSGlobals['OPENREPLAY_USER_ID'];
}
if (isset($JSGlobals['OPENREPLAY_USER_TYPE'])) {
$openreplayUserType = $JSGlobals['OPENREPLAY_USER_TYPE'];
$openreplayMetadata['userType'] = $openreplayUserType;
}
if (isset($JSGlobals['OPENREPLAY_COMPANY_ID'])) {
$openreplayMetadata['companyId'] = $JSGlobals['OPENREPLAY_COMPANY_ID'];
}
// Disable on dev environment if needed
$openreplayEnabled = true;
if (defined('MFAPPNAME') && MFAPPNAME === 'devthetool') {
// Optionally disable on dev - comment out to enable on dev too
// $openreplayEnabled = false;
}
if ($openreplayEnabled):
?>
<script>
var initOpts = {
projectKey: "96MdXVcId8Ph3eOirMWj",
ingestPoint: "https://openreplay.xinon.at/ingest",
defaultInputMode: 2,
obscureTextNumbers: false,
obscureTextEmails: true,
};
var startOpts = { userID: <?= json_encode($openreplayUserId) ?> };
(function(A,s,a,y,e,r){
r=window.OpenReplay=[e,r,y,[s-1, e]];
s=document.createElement('script');s.src=A;s.async=!a;
document.getElementsByTagName('head')[0].appendChild(s);
r.start=function(v){r.push([0])};
r.stop=function(v){r.push([1])};
r.setUserID=function(id){r.push([2,id])};
r.setUserAnonymousID=function(id){r.push([3,id])};
r.setMetadata=function(k,v){r.push([4,k,v])};
r.event=function(k,p,i){r.push([5,k,p,i])};
r.issue=function(k,p){r.push([6,k,p])};
r.isActive=function(){return false};
r.getSessionToken=function(){};
})("//static.openreplay.com/17.0.0/openreplay.js",1,0,initOpts,startOpts);
<?php foreach ($openreplayMetadata as $key => $value): ?>
window.OpenReplay.setMetadata(<?= json_encode($key) ?>, <?= json_encode($value) ?>);
<?php endforeach; ?>
</script>
<?php endif; ?>