templates/components/profile_ctr.js.twig line 1

Open in your IDE?
  1. <script>
  2.     window.increase_shows = function ({profiles, route = '{{ app.request.attributes.get('_route') }}', forceSend = false}) {
  3.         const data = {
  4.             'profiles': profiles,
  5.             'source': route,
  6.             'forceSend': forceSend
  7.         };
  8.         const endpoint = '{{ path("profile_ctr.increase_shows") }}';
  9.         // При закрытии страницы используем sendBeacon для надежной доставки
  10.         if (forceSend && navigator.sendBeacon) {
  11.             const blob = new Blob([JSON.stringify(data)], {type: 'application/json'});
  12.             try {
  13.                 navigator.sendBeacon(endpoint, blob);
  14.                 console.log('Analytics: Данные отправлены через sendBeacon');
  15.                 return Promise.resolve();
  16.             } catch (error) {
  17.                 console.error('Error sendBeacon, falling back to fetch', error);
  18.                 // Fallback to fetch if sendBeacon fails
  19.             }
  20.         }
  21.         // Обычная асинхронная отправка через fetch
  22.         return fetch(endpoint, {
  23.             method: 'POST',
  24.             headers: {
  25.                 "X-Requested-With": "XMLHttpRequest"
  26.             },
  27.             body: JSON.stringify(data)
  28.         }).catch(error => {
  29.             console.error('Error increase_shows', error);
  30.         })
  31.     }
  32.     window.increase_views = function (profileId) {
  33.         fetch('{{ path("profile_ctr.increase_views") }}', {
  34.             method: 'POST',
  35.             headers: {
  36.                 "X-Requested-With": "XMLHttpRequest"
  37.             },
  38.             body: JSON.stringify({
  39.                 'profile': profileId,
  40.             })
  41.         }).catch(error => {
  42.             console.error('Error increase_views', error);
  43.         })
  44.     }
  45. </script>