Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.45 KB | None | 0 0
  1. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-111883079-21"></script>
  2. <script>
  3.   window.dataLayer = window.dataLayer || [];
  4.   function gtag(){dataLayer.push(arguments);}
  5.   gtag('js', new Date());
  6.   gtag('config', 'UA-111883079-21');
  7. </script>
  8.  
  9. <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  10. new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  11. j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  12. 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  13. })(window,document,'script','dataLayer','<?php echo $tracking_gm; ?>');</script>
  14.  
  15.  
  16. <!-- DATA LAYER -->
  17. <script>
  18. $(document).ready(function(){
  19. <?php
  20.  
  21.     // Data Firing Class
  22.     class Analytics {
  23.        
  24.         public static function fireData($type, $upsell = null, $price = null, $product = null) {
  25.            
  26.             switch($type) {
  27.                 case 'vsl-atc':
  28.                     //fire on any 3 order now buttons on vsl
  29.                     //$Analytics->fireData('vsl-atc');
  30.                     ?>
  31.                         dataLayer.push({
  32.                         'event': 'gtm-event',
  33.                         'cat': 'Front End',
  34.                         'act': 'Add to cart',
  35.                         'lab': '<?php echo $product; ?>'
  36.                         });
  37.                         // Facebook
  38.                         //fbq('track', 'InitiateCheckout');
  39.                         alert("DataLayer Fire: VSL-ADD2CART (<?php echo $product; ?>)");
  40.                     <?
  41.                     break;
  42.                 case 'u-purchase':
  43.                     //fire only if user purchases the upsell
  44.                     //$Analytics->fireData('u-purchase', $price, $product);
  45.                     ?>
  46.                         dataLayer.push({
  47.                             'event': 'gtm-event',
  48.                             'cat': 'Purchase',
  49.                             'act': 'Upsell <? echo $upsell[1]; ?> Purchase',
  50.                             'lab': '<? echo $product; ?>', //product is a dynamic variable i.e. 1-bottle, 3-bottles, etc.
  51.                             'value': '<? echo $price; ?>' //price is dynamic based on the price of the purchase on upsell page
  52.                         });
  53.                         /*
  54.                         fbq('track', 'Purchase', {
  55.                             value: '<? echo $price; ?>',
  56.                             currency: 'USD',
  57.                             content_ids: '<? echo $product; ?>',
  58.                             content_type: 'product',
  59.                         });
  60.                         */
  61.                         //alert("DataLayer Fire: Purchase Button Pressed - Price: <?php echo $price; ?> - Product: <?php echo $product; ?> - Upsell <? echo $upsell[1]; ?> Purchase");
  62.                     <?
  63.                     break;
  64.                 case 'u1-success':
  65.                     //fire only when landing on upsell 1 after successful purchase and only fire on first visit
  66.                     //$Analytics->fireData('u1-success', $price, $product);
  67.                     ?>
  68.                         dataLayer.push({
  69.                         'event': 'gtm-event',
  70.                         'cat': 'Purchase',
  71.                         'act': 'First Purchase',
  72.                         'value': '<? echo $price; ?>'
  73.                         });
  74.                         //alert("DataLayer Fire: Upsell 1 First Visit + Total Set ($<?php echo $price; ?>)");
  75.                     <?
  76.                     break;
  77.             }
  78.            
  79.            
  80.         }
  81.     }
  82.  
  83.     // Initialize
  84.     $Analytics = new Analytics();
  85.  
  86.     // Nice Utility Function for Upsell # Detection
  87.     function getStringBetween($str,$from,$to)
  88.     {
  89.         $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
  90.         return substr($sub,0,strpos($sub,$to));
  91.     }
  92.    
  93.     // Get URL
  94.     $pageUrl = $_SERVER['REQUEST_URI'];
  95.  
  96.     // Upsell Or Downsell
  97.     if(!preg_match('/(d1-)/', $pageUrl)) {
  98.         // Get Upsell #
  99.         $upsell_number = getStringBetween($pageUrl, '/u', '-');
  100.     } else {
  101.         $downsell_number = getStringBetween($pageUrl, '/d', '-'); // Usually will be 1, but incase this changes...
  102.     }
  103.  
  104.     // Vsl Page Check
  105.     if(!preg_match('/(d1-|u0-|u1-|u2-|u3-)/', $pageUrl)) {
  106.        
  107.         // Is VSL
  108.         ?>
  109.             // All CTA Buttons
  110.             $(".orderbutton-1").click(function(){
  111.                 <?php
  112.                 $Analytics->fireData('vsl-cta', '', '', $pc_main_1);
  113.                 ?>
  114.             });
  115.             $(".orderbutton-3").click(function(){
  116.                 <?php
  117.                 $Analytics->fireData('vsl-cta', '', '', $pc_main_3);
  118.                 ?>
  119.             });
  120.             $(".orderbutton-6").click(function(){
  121.                 <?php
  122.                 $Analytics->fireData('vsl-cta', '', '', $pc_main_6);
  123.                 ?>
  124.             });
  125.    
  126.    
  127.         <?php
  128.  
  129.     } else {
  130.        
  131.         // Is Upsell/Downsell
  132.         if(!$downsell_number){
  133.            
  134.             // Upsell
  135.             // Check If its 3 Buttons or 2 Buttons.
  136.             if(${"upsell" . $upsell_number . "c_price"}){
  137.                 // 3 Buttons
  138.                 ?>
  139.                 $('.orderbutton-1').click(function(){
  140.                     <?php
  141.                     $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "a_price"}, ${"pc_u" . $upsell_number . "_1"});
  142.                     ?>
  143.                 });
  144.                 $('.orderbutton-3').click(function(){
  145.                     <?php
  146.                     $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "b_price"}, ${"pc_u" . $upsell_number . "_3"});
  147.                     ?>
  148.                 });
  149.                 $('.orderbutton-6').click(function(){
  150.                     <?php
  151.                     $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "c_price"}, ${"pc_u" . $upsell_number . "_6"});
  152.                     ?>
  153.                 });
  154.                 <?
  155.             } elseif(${"upsell" . $upsell_number . "a_price"} && ${"upsell" . $upsell_number . "b_price"}) {
  156.                 // 2 Buttons.
  157.                 ?>
  158.                 $('.orderbutton-3').click(function(){
  159.                     <?php
  160.                     $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "a_price"}, ${"pc_u" . $upsell_number . "_3"});
  161.                     ?>
  162.                 });
  163.                 $('.orderbutton-6').click(function(){
  164.                     <?php
  165.                     $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "b_price"}, ${"pc_u" . $upsell_number . "_6"});
  166.                     ?>
  167.                 });
  168.                 <?
  169.             } else {
  170.                 // 1 Button
  171.                 ?>
  172.                 // It doesn't, single button.
  173.                 $(".order-button").click(function(){
  174.                     <?php
  175.                         $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "_price"}, ${"purchase_codes_u" . $upsell_number});
  176.                     ?>
  177.                 });
  178.                 <?
  179.             }
  180.         } else {
  181.             // Downsell
  182.             ?>
  183.             $(".orderbutton").click(function(){
  184.                 <?php
  185.                     $Analytics->fireData('u-purchase', "d" . $downsell_number, ${"downsell" . $downsell_number . "_price"}, ${"purchase_codes_d" . $downsell_number});
  186.                 ?>
  187.             });
  188.             <?
  189.         }
  190.     }
  191.        
  192.     // Upsell 1 Initial Fire.
  193.     if(preg_match('/(u1-)/', $pageUrl)) {
  194.  
  195.         //fire only when landing on upsell 1 after successful purchase and only fire on first visit
  196.         if(!isset($_COOKIE[$productDir . '_u1']) && isset($_REQUEST['total'])){
  197.            
  198.             // Set Cookie Script
  199.             ?>
  200.                 // Set Upsell 1 Cookie
  201.                 function setCookie(cname, cvalue, exdays) {
  202.                     var d = new Date();
  203.                     d.setTime(d.getTime() + (exdays*24*60*60*1000));
  204.                     var expires = "expires="+ d.toUTCString();
  205.                     document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  206.                 }
  207.                 setTimeout(setCookie('<?php echo $productDir . '_u1'; ?>','set',30),1000);
  208.             <?
  209.  
  210.             // Datalayer Fire.
  211.             $Analytics->fireData('u1-success', $price, $_REQUEST['total']);
  212.         }
  213.     }
  214.    
  215. ?>
  216. });
  217. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement