Skip to content

Commit 2c34ad8

Browse files
committed
added function for calling admin-suite api for tracking visitors
1 parent e818016 commit 2c34ad8

7 files changed

Lines changed: 134 additions & 1 deletion

File tree

.env.dev

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ EXTERNAL_LOG_TOKEN=cbd8b08271wefg
2121
METRICS_EXPORTER_ENABLED=true
2222
METRICS_EXPORTER_ALLOWED_IP=% # IP address with access to metrics (use % for all addresses)
2323

24+
# enable visitor tracking
25+
VISITOR_TRACKING_ENABLED=false
26+
VISITOR_TRACKING_URL=https://admin.becvar.xyz/api/monitoring/visitor/tracking
27+
2428
# database config
2529
DATABASE_DRIVER=pdo_mysql
2630
DATABASE_VERSION=8.0.43

.env.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ EXTERNAL_LOG_TOKEN=cbd8b08271wefg
2121
METRICS_EXPORTER_ENABLED=true
2222
METRICS_EXPORTER_ALLOWED_IP=172.18.0.1 # IP address with access to metrics (use % for all addresses)
2323

24+
# enable visitor tracking
25+
VISITOR_TRACKING_ENABLED=false
26+
VISITOR_TRACKING_URL=https://admin.becvar.xyz/api/monitoring/visitor/tracking
27+
2428
# database config
2529
DATABASE_DRIVER=pdo_mysql
2630
DATABASE_VERSION=8.0.43

config/services.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ services:
1515
tags:
1616
- { name: kernel.event_subscriber }
1717

18+
# twig extensions
19+
# --------------------------------------------------------------------------------- #
20+
# app util extension
21+
App\Twig\AppUtilExtension:
22+
arguments:
23+
$appUtil: '@App\Util\AppUtil'
24+
tags: [ 'twig.extension' ]
25+
1826
# app middlewares
1927
# --------------------------------------------------------------------------------- #
2028
# check if assets are builded

src/Twig/AppUtilExtension.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Twig;
4+
5+
use App\Util\AppUtil;
6+
use Twig\TwigFunction;
7+
use Twig\Extension\AbstractExtension;
8+
9+
/**
10+
* Class AppUtilExtension
11+
*
12+
* Extension for providing AppUtil methods
13+
*
14+
* @package App\Twig
15+
*/
16+
class AppUtilExtension extends AbstractExtension
17+
{
18+
private AppUtil $appUtil;
19+
20+
public function __construct(AppUtil $appUtil)
21+
{
22+
$this->appUtil = $appUtil;
23+
}
24+
25+
/**
26+
* Get twig functions from AppUtil
27+
*
28+
* getEnvValue = getEnvValue in AppUtil
29+
*
30+
* @return TwigFunction[] Array of TwigFunction objects
31+
*/
32+
public function getFunctions(): array
33+
{
34+
return [
35+
new TwigFunction('getEnvValue', [$this->appUtil, 'getEnvValue'])
36+
];
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Tests\Twig;
4+
5+
use App\Util\AppUtil;
6+
use App\Twig\AppUtilExtension;
7+
use PHPUnit\Framework\TestCase;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
10+
/**
11+
* Class AppUtilExtensionTest
12+
*
13+
* Test cases for app util twig extension
14+
*
15+
* @package App\Tests\Twig
16+
*/
17+
class AppUtilExtensionTest extends TestCase
18+
{
19+
private AppUtil & MockObject $appUtil;
20+
private AppUtilExtension $appUtilExtension;
21+
22+
protected function setUp(): void
23+
{
24+
$this->appUtil = $this->getMockBuilder(AppUtil::class)->disableOriginalConstructor()->getMock();
25+
$this->appUtilExtension = new AppUtilExtension($this->appUtil);
26+
}
27+
28+
/**
29+
* Test get functions
30+
*
31+
* @return void
32+
*/
33+
public function testGetFunctions(): void
34+
{
35+
// call tested method
36+
$functions = $this->appUtilExtension->getFunctions();
37+
38+
// assert result
39+
$this->assertCount(1, $functions);
40+
41+
// check getEnvValue function
42+
$this->assertEquals('getEnvValue', $functions[0]->getName());
43+
$this->assertEquals([$this->appUtil, 'getEnvValue'], $functions[0]->getCallable());
44+
}
45+
}

view/common/base.twig

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Code paste</title>
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
76
<meta content="Text and code sharing tool" name="description">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
88
<link rel="icon" href={{ asset('assets/images/save.svg') }} type="image/x-icon"/>
99

1010
{# REDIRECT TO ERROR PAGE IF JAVASCRIPT IS DISABLED #}
@@ -25,5 +25,22 @@
2525
{% block body %}{% endblock %}
2626
</main>
2727
{{ encore_entry_script_tags('loading-js') }}
28+
29+
{# VISITOR TRACKING SYSTEM #}
30+
{% if getEnvValue('VISITOR_TRACKING_ENABLED') == 'true' %}
31+
<script>
32+
const visitorReferer = document.referrer || 'Unknown'
33+
fetch('{{ getEnvValue("VISITOR_TRACKING_URL") }}', {
34+
method: 'POST',
35+
headers: {
36+
'Content-Type': 'application/json'
37+
},
38+
body: JSON.stringify({
39+
service_name: 'paste.becvar.xyz',
40+
referer: visitorReferer
41+
})
42+
})
43+
</script>
44+
{% endif %}
2845
</body>
2946
</html>

view/common/error-base.twig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,22 @@
1111
</head>
1212
<body>
1313
{% block body %}{% endblock %}
14+
15+
{# VISITOR TRACKING SYSTEM #}
16+
{% if getEnvValue('VISITOR_TRACKING_ENABLED') == 'true' %}
17+
<script>
18+
const visitorReferer = document.referrer || 'Unknown'
19+
fetch('{{ getEnvValue("VISITOR_TRACKING_URL") }}', {
20+
method: 'POST',
21+
headers: {
22+
'Content-Type': 'application/json'
23+
},
24+
body: JSON.stringify({
25+
service_name: 'paste.becvar.xyz',
26+
referer: visitorReferer
27+
})
28+
})
29+
</script>
30+
{% endif %}
1431
</body>
1532
</html>

0 commit comments

Comments
 (0)