Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script async src="https://www.googletagmanager.com/gtag/js?id=UA-111883079-21"></script>
- <script>
- window.dataLayer = window.dataLayer || [];
- function gtag(){dataLayer.push(arguments);}
- gtag('js', new Date());
- gtag('config', 'UA-111883079-21');
- </script>
- <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
- new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
- j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
- 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
- })(window,document,'script','dataLayer','<?php echo $tracking_gm; ?>');</script>
- <!-- DATA LAYER -->
- <script>
- $(document).ready(function(){
- <?php
- // Data Firing Class
- class Analytics {
- public static function fireData($type, $upsell = null, $price = null, $product = null) {
- switch($type) {
- case 'vsl-atc':
- //fire on any 3 order now buttons on vsl
- //$Analytics->fireData('vsl-atc');
- ?>
- dataLayer.push({
- 'event': 'gtm-event',
- 'cat': 'Front End',
- 'act': 'Add to cart',
- 'lab': '<?php echo $product; ?>'
- });
- // Facebook
- //fbq('track', 'InitiateCheckout');
- alert("DataLayer Fire: VSL-ADD2CART (<?php echo $product; ?>)");
- <?
- break;
- case 'u-purchase':
- //fire only if user purchases the upsell
- //$Analytics->fireData('u-purchase', $price, $product);
- ?>
- dataLayer.push({
- 'event': 'gtm-event',
- 'cat': 'Purchase',
- 'act': 'Upsell <? echo $upsell[1]; ?> Purchase',
- 'lab': '<? echo $product; ?>', //product is a dynamic variable i.e. 1-bottle, 3-bottles, etc.
- 'value': '<? echo $price; ?>' //price is dynamic based on the price of the purchase on upsell page
- });
- /*
- fbq('track', 'Purchase', {
- value: '<? echo $price; ?>',
- currency: 'USD',
- content_ids: '<? echo $product; ?>',
- content_type: 'product',
- });
- */
- //alert("DataLayer Fire: Purchase Button Pressed - Price: <?php echo $price; ?> - Product: <?php echo $product; ?> - Upsell <? echo $upsell[1]; ?> Purchase");
- <?
- break;
- case 'u1-success':
- //fire only when landing on upsell 1 after successful purchase and only fire on first visit
- //$Analytics->fireData('u1-success', $price, $product);
- ?>
- dataLayer.push({
- 'event': 'gtm-event',
- 'cat': 'Purchase',
- 'act': 'First Purchase',
- 'value': '<? echo $price; ?>'
- });
- //alert("DataLayer Fire: Upsell 1 First Visit + Total Set ($<?php echo $price; ?>)");
- <?
- break;
- }
- }
- }
- // Initialize
- $Analytics = new Analytics();
- // Nice Utility Function for Upsell # Detection
- function getStringBetween($str,$from,$to)
- {
- $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
- return substr($sub,0,strpos($sub,$to));
- }
- // Get URL
- $pageUrl = $_SERVER['REQUEST_URI'];
- // Upsell Or Downsell
- if(!preg_match('/(d1-)/', $pageUrl)) {
- // Get Upsell #
- $upsell_number = getStringBetween($pageUrl, '/u', '-');
- } else {
- $downsell_number = getStringBetween($pageUrl, '/d', '-'); // Usually will be 1, but incase this changes...
- }
- // Vsl Page Check
- if(!preg_match('/(d1-|u0-|u1-|u2-|u3-)/', $pageUrl)) {
- // Is VSL
- ?>
- // All CTA Buttons
- $(".orderbutton-1").click(function(){
- <?php
- $Analytics->fireData('vsl-cta', '', '', $pc_main_1);
- ?>
- });
- $(".orderbutton-3").click(function(){
- <?php
- $Analytics->fireData('vsl-cta', '', '', $pc_main_3);
- ?>
- });
- $(".orderbutton-6").click(function(){
- <?php
- $Analytics->fireData('vsl-cta', '', '', $pc_main_6);
- ?>
- });
- <?php
- } else {
- // Is Upsell/Downsell
- if(!$downsell_number){
- // Upsell
- // Check If its 3 Buttons or 2 Buttons.
- if(${"upsell" . $upsell_number . "c_price"}){
- // 3 Buttons
- ?>
- $('.orderbutton-1').click(function(){
- <?php
- $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "a_price"}, ${"pc_u" . $upsell_number . "_1"});
- ?>
- });
- $('.orderbutton-3').click(function(){
- <?php
- $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "b_price"}, ${"pc_u" . $upsell_number . "_3"});
- ?>
- });
- $('.orderbutton-6').click(function(){
- <?php
- $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "c_price"}, ${"pc_u" . $upsell_number . "_6"});
- ?>
- });
- <?
- } elseif(${"upsell" . $upsell_number . "a_price"} && ${"upsell" . $upsell_number . "b_price"}) {
- // 2 Buttons.
- ?>
- $('.orderbutton-3').click(function(){
- <?php
- $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "a_price"}, ${"pc_u" . $upsell_number . "_3"});
- ?>
- });
- $('.orderbutton-6').click(function(){
- <?php
- $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "b_price"}, ${"pc_u" . $upsell_number . "_6"});
- ?>
- });
- <?
- } else {
- // 1 Button
- ?>
- // It doesn't, single button.
- $(".order-button").click(function(){
- <?php
- $Analytics->fireData('u-purchase', "u" . $upsell_number, ${"upsell" . $upsell_number . "_price"}, ${"purchase_codes_u" . $upsell_number});
- ?>
- });
- <?
- }
- } else {
- // Downsell
- ?>
- $(".orderbutton").click(function(){
- <?php
- $Analytics->fireData('u-purchase', "d" . $downsell_number, ${"downsell" . $downsell_number . "_price"}, ${"purchase_codes_d" . $downsell_number});
- ?>
- });
- <?
- }
- }
- // Upsell 1 Initial Fire.
- if(preg_match('/(u1-)/', $pageUrl)) {
- //fire only when landing on upsell 1 after successful purchase and only fire on first visit
- if(!isset($_COOKIE[$productDir . '_u1']) && isset($_REQUEST['total'])){
- // Set Cookie Script
- ?>
- // Set Upsell 1 Cookie
- function setCookie(cname, cvalue, exdays) {
- var d = new Date();
- d.setTime(d.getTime() + (exdays*24*60*60*1000));
- var expires = "expires="+ d.toUTCString();
- document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
- }
- setTimeout(setCookie('<?php echo $productDir . '_u1'; ?>','set',30),1000);
- <?
- // Datalayer Fire.
- $Analytics->fireData('u1-success', $price, $_REQUEST['total']);
- }
- }
- ?>
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement