src/Twig/GoogleAnalyticsEvents.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\DTO\AppDTO;
  4. use App\Entity\Prod;
  5. use App\Env;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFunction;
  9. class GoogleAnalyticsEvents extends AbstractExtension
  10. {
  11.     public function __construct(
  12.         private RequestStack $requestStack,
  13.         private AppDTO $app,
  14.     )
  15.     {
  16.         
  17.     }
  18.     public function getFunctions(): array
  19.     {
  20.         return [
  21.             new TwigFunction('ga_events_init', [$this'init']),
  22.             new TwigFunction('ga_events_search', [$this'searchResults']),
  23.             new TwigFunction('ga_events_prodlist', [$this'prodList']),
  24.             new TwigFunction('ga_events_prodcont', [$this'prodCont']),
  25.             new TwigFunction('ga_events_addtocart', [$this'addToCart']),
  26.             new TwigFunction('ga_events_checkout_begin', [$this'checkoutBegin']),
  27.             new TwigFunction('ga_events_checkout_finish', [$this'checkoutFinish']),
  28.             new TwigFunction('ga_events_checkout_client_email', [$this'clientEmail']),                        
  29.         ];
  30.     }
  31.     public function getName(): string
  32.     {
  33.         return 'app_google_analytics_extension';
  34.     }
  35.     public function init(): string
  36.     {
  37.         $url $this->requestStack->getCurrentRequest()->getPathInfo();
  38.         if (strstr($url'checkout/completed') || strstr($url'checkout/finish')) {
  39.             return '<script>
  40.                 window.dataLayer = window.dataLayer || [];
  41.                 function gtag() {
  42.                 window.dataLayer.push(arguments);
  43.                 }
  44.                 gtag("consent", "default", {  "ad_storage": "granted",
  45.                 "ad_user_data": "granted",
  46.                 "ad_personalization": "granted",
  47.                 "analytics_storage": "granted"
  48.                 });
  49.                 </script>
  50.                 <script async src="https://www.googletagmanager.com/gtag/js?id='.Env::code_gtm().'"></script>
  51.                 <script>
  52.                 window.dataLayer = window.dataLayer || [];
  53.                 function gtag() {
  54.                 window.dataLayer.push(arguments);
  55.                 }
  56.                 gtag("js", new Date());
  57.                 gtag("config", "'.Env::code_gtm().'", {allow_enhanced_conversions: true});
  58.             </script>';
  59.         } else {
  60.             return '<script>
  61.                 window.dataLayer = window.dataLayer || [];
  62.                 function gtag() {
  63.                 window.dataLayer.push(arguments);
  64.                 }
  65.                 gtag("consent", "default", {
  66.                 "ad_storage": "granted",
  67.                 "ad_user_data": "granted",
  68.                 "ad_personalization": "granted",
  69.                 "analytics_storage": "granted"
  70.                 });
  71.                 </script>
  72.                 <script async src="https://www.googletagmanager.com/gtag/js?id='.Env::code_gtm().'"></script>
  73.                 <script>
  74.                 window.dataLayer = window.dataLayer || [];
  75.                 function gtag() {
  76.                 window.dataLayer.push(arguments);
  77.                 }
  78.                 gtag("js", new Date());
  79.                 gtag("config", "'.Env::code_gtm().'");
  80.             </script>';
  81.         }
  82.         
  83.     }
  84.     /**
  85.      * 
  86.      * @param string $q 
  87.      * @param \App\Entity\Prod[] $prods 
  88.      * @return void 
  89.      */
  90.     public function searchResults(string $q, array $prods)
  91.     {
  92.         $sum 0;
  93.         $i 0;
  94.         $i 0;
  95.         
  96.         foreach ($prods as $prod) {
  97.             $i++;
  98.             if ($i 10) {
  99.                 break;
  100.             }
  101.             $sum += $prod->getPrice() * (100 $prod->getSkidka()) / 100;
  102.         }
  103.         $result '<script>
  104.         gtag("event", "view_search_results", {
  105.             search_term: "'.$q.'",
  106.             currency: "'.Env::currency_code().'",
  107.             value: '.round($sumEnv::price_precission()).',
  108.             items: [';
  109.         $i 0;
  110.         foreach ($prods as $prod) {
  111.             $i++;
  112.             if ($i 10) {
  113.                 break;
  114.             }
  115.             $result .= '
  116.                 {
  117.                     id: "'.$prod->getId().'-'.$this->requestStack->getCurrentRequest()->getLocale().'",
  118.                     name: "'.$prod->getName().'",
  119.                     price: '.round(($prod->getPrice() * (100 $prod->getSkidka()) / 100), Env::price_precission()).',
  120.                     google_business_vertical: "retail"
  121.                 }';
  122.             if ($i 9) {
  123.                 $result .= ',';
  124.             }
  125.         }
  126.         $result .= '
  127.             ]
  128.         });</script>';
  129.         
  130.         return $result;
  131.     }
  132.     /**
  133.      * 
  134.      * @param \App\Entity\Prod[] $prods 
  135.      * @return void 
  136.      */
  137.     public function prodList(array $prods)
  138.     {
  139.         $sum 0;
  140.         $i 0;
  141.         foreach ($prods as $prod) {
  142.             $i++;
  143.             if ($i 10) {
  144.                 break;
  145.             }
  146.             $sum += $prod->getPrice() * (100 $prod->getSkidka()) / 100;
  147.         }
  148.         $result '<script>gtag("event", "view_item_list", {
  149.             currency: "'.Env::currency_code().'",
  150.             value: '.round($sumEnv::price_precission()).',
  151.             items: [';
  152.         $i 0;
  153.         foreach ($prods as $prod) {
  154.             $i++;
  155.             if ($i 10) {
  156.                 break;
  157.             }
  158.             $result .= '
  159.                 {
  160.                     id: "'.$prod->getId().'-'.$this->requestStack->getCurrentRequest()->getLocale().'",
  161.                     name: "'.$prod->getName().'",
  162.                     price: '.round(($prod->getPrice() * (100 $prod->getSkidka()) / 100), Env::price_precission()).',
  163.                     google_business_vertical: "retail"
  164.                 }';
  165.             if ($i 9) {
  166.                 $result .= ',';
  167.             }
  168.         }
  169.         $result .= '
  170.             ]
  171.         });</script>';
  172.         
  173.         return $result;
  174.     }
  175.     /**
  176.      * 
  177.      * @param \App\Entity\Prod $prod
  178.      * @return void 
  179.      */
  180.     public function prodCont(Prod $prod)
  181.     {
  182.         $result '<script>gtag("event", "view_item", {
  183.             currency: "'.Env::currency_code().'",
  184.             value: '.round(($prod->getPrice() * (100 $prod->getSkidka()) / 100), Env::price_precission()).',
  185.             items: [{
  186.                     id: "'.$prod->getId().'-'.$this->requestStack->getCurrentRequest()->getLocale().'",
  187.                     name: "'.$prod->getName().'",
  188.                     price: '.round(($prod->getPrice() * (100 $prod->getSkidka()) / 100), Env::price_precission()).',
  189.                     google_business_vertical: "retail"
  190.                 }]
  191.             });</script>';
  192.         
  193.         return $result;
  194.     }
  195.     /**
  196.      * 
  197.      * @param \App\Entity\Prod $prod
  198.      * @return void 
  199.      */
  200.     public function addToCart(Prod $prod)
  201.     {
  202.         $result 'gtag("event", "add_to_cart", {
  203.             currency: "'.Env::currency_code().'",
  204.             value: '.($prod->getPrice() * (100 $prod->getSkidka()) / 100).',
  205.             items: [{
  206.                     id: "'.$prod->getId().'-'.$this->requestStack->getCurrentRequest()->getLocale().'",
  207.                     name: "'.$prod->getName().'",
  208.                     price: '.round(($prod->getPrice() * (100 $prod->getSkidka()) / 100), Env::price_precission()).',
  209.                     google_business_vertical: "retail"
  210.                 }]
  211.             });';
  212.         
  213.         return $result;
  214.     }
  215.     /**
  216.      * 
  217.      * @param array $prods 
  218.      * @return void 
  219.      */
  220.     public function checkoutBegin(array $cart_items)
  221.     {
  222.         $sum 0;
  223.         $total count($cart_items);
  224.         $i 0;
  225.         foreach ($cart_items as $v) {
  226.             /** @var \App\Entity\Prod $prod */
  227.             $prod $v['prod'];
  228.             //TODO. Какую цену тут показывать?!
  229.             $sum += $v['price'] * $v['num'];
  230.         }
  231.         $result '<script>gtag("event", "begin_checkout", {
  232.             currency: "'.Env::currency_code().'",
  233.             value: '.round($sumEnv::price_precission()).',
  234.             items: [';
  235.         foreach ($cart_items as $v) {
  236.             /** @var \App\Entity\Prod $prod */
  237.             $prod $v['prod'];
  238.             $i++;
  239.             $result .= '
  240.                 {
  241.             
  242.                     id: "'.$prod->getId().'-'.$this->requestStack->getCurrentRequest()->getLocale().'",
  243.                     name: "'.$prod->getName().'",
  244.                     price: '.round($v['price'], Env::price_precission()).',
  245.                     quantity: '.$v['num'].',
  246.                     discount: '.intval($v['skidka']).',
  247.                     google_business_vertical: "retail"
  248.                 }';
  249.             if ($i $total) {
  250.                 $result .= ',';
  251.             }
  252.         }
  253.         $result .= '
  254.             ]
  255.         });</script>';
  256.         
  257.         return $result;
  258.     }
  259.     public function checkoutFinish(int $order_idfloat $amount, array $cart_items)
  260.     {
  261.         $result '<script>gtag("event", "purchase", {
  262.             send_to: [
  263.                 "'.Env::code_gtm().'"
  264.             ],
  265.             transaction_id: "'.$order_id.'",
  266.             currency: "'.Env::currency_code().'",
  267.             value: '.round($amountEnv::price_precission()).',
  268.             aw_merchant_id: '.Env::merchant_id().',
  269.             aw_feed_country: "'.Env::country_code().'",
  270.             aw_feed_language: "'.$this->requestStack->getCurrentRequest()->getLocale().'",
  271.             items: [';
  272.         $total count($cart_items);
  273.         $i 0;
  274.         foreach ($cart_items as $v) {
  275.             /** @var \App\Entity\Prod $prod */
  276.             $prod $v['prod'];
  277.             $i++;
  278.             $result .= '
  279.                 {
  280.                     id: "'.$prod->getId().'-'.$this->requestStack->getCurrentRequest()->getLocale().'",
  281.                     name: "'.$prod->getName().'",
  282.                     price: '.round($v['price'], Env::price_precission()).',
  283.                     quantity: '.$v['num'].',
  284.                     discount: '.intval($v['skidka']).',
  285.                     google_business_vertical: "retail"
  286.                 }';
  287.             if ($i $total) {
  288.                 $result .= ',';
  289.             }
  290.         }
  291.         $result .= '
  292.             ]
  293.         });</script>';
  294.         return $result;
  295.     }
  296.     public function clientEmail(string $email) {
  297.         return '<script>gtag("set", "user_data", {
  298.             email: "'.$email.'"
  299.         });</script>';
  300.     }
  301. }