Add Yandex Metrika integration for tracking; update FindSliderBySlugTask to ensure active slides are filtered correctly based on time; enhance app.blade.php for conditional Yandex Metrika script inclusion based on environment.

This commit is contained in:
F4ilji
2025-09-27 22:35:36 +05:00
parent 02fb2554c1
commit 72af09d93d
6 changed files with 51 additions and 16 deletions
@@ -4,7 +4,7 @@ namespace App\Containers\Widget\Tasks;
use App\Containers\Widget\Models\Slider; use App\Containers\Widget\Models\Slider;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon; // Убедитесь, что Carbon импортирован
class FindSliderBySlugTask class FindSliderBySlugTask
{ {
@@ -13,7 +13,7 @@ class FindSliderBySlugTask
$cacheKey = 'slider_' . $slug; $cacheKey = 'slider_' . $slug;
return Cache::remember($cacheKey, now()->addHour(), function () use ($slug) { return Cache::remember($cacheKey, now()->addHour(), function () use ($slug) {
$now = Carbon::now(); // Получаем текущее время один раз $now = Carbon::now();
$slider = Slider::query() $slider = Slider::query()
->where('slug', $slug) ->where('slug', $slug)
@@ -21,7 +21,13 @@ class FindSliderBySlugTask
->with(['slides' => function($query) use ($now) { ->with(['slides' => function($query) use ($now) {
$query->where('is_active', true) $query->where('is_active', true)
->where('start_time', '<=', $now) ->where('start_time', '<=', $now)
->where('end_time', '>=', $now); // Добавляем условие для end_time
->where(function ($q) use ($now) {
// Показываем слайд, если end_time еще не наступил
$q->where('end_time', '>=', $now)
// ИЛИ если end_time вообще не указан (NULL)
->orWhereNull('end_time');
});
}]) }])
->firstOrFail(); ->firstOrFail();
@@ -39,6 +39,7 @@ class HandleInertiaRequests extends Middleware
} }
return 'empty'; return 'empty';
}, },
'yandex_metrika_id' => config('services.yandex_metrika.id'),
]; ];
} }
} }
@@ -44,6 +44,7 @@ class HandleInertiaRequests extends Middleware
} }
return 'empty'; return 'empty';
}, },
'yandex_metrika_id' => config('services.yandex_metrika.id'),
]; ];
} }
} }
+4
View File
@@ -31,4 +31,8 @@ return [
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
], ],
'yandex_metrika' => [
'id' => env('YANDEX_METRIKA_ID'),
],
]; ];
+10 -6
View File
@@ -5,22 +5,17 @@ import 'virtual:svg-icons-register'
import { createSSRApp, h } from 'vue'; import { createSSRApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/vue3'; import { createInertiaApp, router } from '@inertiajs/vue3';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m'; import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';
import {linksReform} from "@/mixins/LinksReform.js"; import {linksReform} from "@/mixins/LinksReform.js";
import cookieMixin from "@/mixins/cookieMixin.js"; import cookieMixin from "@/mixins/cookieMixin.js";
import store from '@/store/index.js'; import store from '@/store/index.js';
import '@vuepic/vue-datepicker/dist/main.css'; import '@vuepic/vue-datepicker/dist/main.css';
// const appName = import.meta.env.VITE_APP_NAME || 'НТГСПИ';
createInertiaApp({ createInertiaApp({
resolve: name => { resolve: name => {
const pages = import.meta.glob('./Pages/**/*.vue') const pages = import.meta.glob('./Pages/**/*.vue')
return pages[`./Pages/${name}.vue`]() return pages[`./Pages/${name}.vue`]()
// resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue'))
}, },
setup({ el, App, props, plugin }) { setup({ el, App, props, plugin }) {
return createSSRApp({ render: () => h(App, props) }) return createSSRApp({ render: () => h(App, props) })
@@ -37,4 +32,13 @@ createInertiaApp({
}, },
}); });
router.on('navigate', (event) => {
const metrikaId = event.detail.page.props.yandex_metrika_id;
if (metrikaId && typeof ym === 'function') {
ym(metrikaId, 'hit', window.location.href, {
title: document.title,
referer: event.detail.page.props.ziggy?.previous_url || document.referrer,
});
}
});
+26 -7
View File
@@ -6,7 +6,7 @@
@if (env('NGINX_ENV') === 'prod') @if (env('NGINX_ENV') === 'prod')
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
@endif @endif
{{-- <title inertia>{{ config('app.name', 'Laravel') }}</title>--}} {{-- <title inertia>{{ config('app.name', 'Laravel') }}</title>--}}
<!-- Fonts --> <!-- Fonts -->
<link rel="preconnect" href="https://fonts.bunny.net"> <link rel="preconnect" href="https://fonts.bunny.net">
@@ -49,13 +49,32 @@
scroll-padding: 6rem; scroll-padding: 6rem;
scroll-behavior: smooth !important; scroll-behavior: smooth !important;
} }
</style> </style>
<body class=""> <body class="">
@inertia @inertia
</body>
</html> @if(config('services.yandex_metrika.id') && app()->environment('production'))
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
(function(m,e,t,r,i,k,a){
m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)
})(window, document,'script','https://mc.yandex.ru/metrika/tag.js', 'ym');
ym({{ config('services.yandex_metrika.id') }}, 'init', {
defer: true,
webvisor:true,
clickmap:true,
ecommerce:"dataLayer",
accurateTrackBounce:true,
trackLinks:true
});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/{{ config('services.yandex_metrika.id') }}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
@endif
</body>
</html>