Page 4 of 4 FirstFirst ... 234
Results 31 to 32 of 32
  1. #31
    Join Date
    Jan 2015
    Posts
    462
    Plugin Contributions
    0

    Default Re: Google Dynamic Remarketing Mod

    I have a question

    I noticed on the checkout success page the script does not capture the shipping or tax in the "ecomm_totalvalue".
    I am not sure if we are supposed to even collect that information. Any one have any advice...

    I also made some other revisions to the script.

    Code:
    <?php
    // Google Remarketing
    // Dynamic Remarketing tracking code
    // For Zen-Cart 1.5
    // Author: Stef van Dijk - vandijkstef.nl
    
    // This module is delivered as is, without any warranty
    // Feel free to use and edit this module
    // Licensed: GPL - http://www.gnu.org/licenses/licenses.html#GPL
    
    // NOTE: This script only handles products identified by ID or by model-number.
    // It does NOT differentiate products by attribute, nor by other properties such as custom fields like SKU, etc.
    
    // Google conversion ID -- Replace AW-xxxxxxxxxx with your own ID received from Google:
    $ga_conversion_id = 'AW-xxxxxxxxxx';
    
    $product_identifier = 'id'; // can be 'id' or 'model', for the store's product_id or product_model_number respectively
    
    // Pages to ignore
    $pages_to_ignore = [
        'account', 'account_edit', 'account_history', 'account_history_info', 'account_newsletters',
        'account_notifications', 'account_password', 'address_book', 'address_book_process',
        'advanced_search', 'ask_a_question', 'checkout_confirmation', 'checkout_payment',
        'checkout_payment_address', 'checkout_process', 'checkout_shipping', 'checkout_shipping_address',
        'cookie_usage', 'create_account', 'create_account_success', 'customers_authorization',
        'discount_coupon', 'down_for_maintenance', 'download', 'download_time_out', 'gv_redeem',
        'gv_send', 'info_shopping_cart', 'login', 'logoff', 'page_2', 'page_3', 'page_4',
        'page_not_found', 'password_forgotten', 'payer_auth_auth', 'payer_auth_frame',
        'payer_auth_verifier', 'popup_attributes_qty_prices', 'popup_coupon_help', 'popup_cvv_help',
        'popup_image', 'popup_image_additional', 'popup_search_help', 'popup_shipping_estimator',
        'product_reviews_write', 'redirect', 'shippinginfo', 'ssl_check', 'time_out', 'unsubscribe',
        'search', 'checkout_one', 'checkout_one_confirmation', 'order_status', 'privacy', 'conditions',
        'site_map', 'contact_us', 'returns', 'page'
    ];
    
    // Check if the current page is in the ignore list
    if (in_array($current_page_base, $pages_to_ignore)) {
        return;
    }
    
    // DO NOT CHANGE BELOW
    if (empty($ga_conversion_id) || $ga_conversion_id === '123456789') {
        return;
    }
    
    // Product IDs array
    $ecomm_prodid = [];
    $ecomm_totalvalue = 0;
    $ecomm_pagetype = 'other';
    
    // Helper function to get product price
    function getProductPrice($product_id) {
        $product_price_html = zen_get_products_display_price($product_id);
        if (preg_match('/<span[^>]*class="[^"]*productSpecialPrice[^"]*"[^>]*>\$([\d,]+\.\d{2})<\/span>/', $product_price_html, $matches)) {
            return (float)str_replace(',', '', $matches[1]);
        } elseif (preg_match('/<span[^>]*class="[^"]*normalprice[^"]*"[^>]*>\$([\d,]+\.\d{2})<\/span>/', $product_price_html, $matches)) {
            return (float)str_replace(',', '', $matches[1]);
        }
        return 0;
    }
    
    // If on a product page, get the product id
    if ($current_page_base === 'product_info' && !empty($_GET['products_id'])) {
        $product_id = (int)$_GET['products_id'];
        $ecomm_prodid[] = (string)$product_id;
        $ecomm_totalvalue = getProductPrice($product_id);
    }
    
    // If on the shopping cart page, add the cart's items to the array
    if ($current_page_base === 'shopping_cart') {
        $cart_products = $_SESSION['cart']->get_products();
        foreach ($cart_products as $product) {
            $ecomm_prodid[] = $product_identifier === 'id' ? (string)(int)$product['id'] : (string)$product['model'];
        }
        $ecomm_totalvalue = $_SESSION['cart']->show_total();
    }
    
    // If this is checkout_success, set the products and total value from order
    if ($current_page_base === 'checkout_success') {
        $order_id = isset($_SESSION['order_number_created']) ? (int)$_SESSION['order_number_created'] : (isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0);
        if ($order_id > 0) {
            $order = $db->Execute("SELECT products_id, products_model, final_price, products_quantity FROM " . TABLE_ORDERS_PRODUCTS . " WHERE orders_id = " . $order_id);
            while (!$order->EOF) {
                $ecomm_prodid[] = $product_identifier === 'id' ? (string)(int)$order->fields['products_id'] : (string)$order->fields['products_model'];
                $ecomm_totalvalue += (float)$order->fields['final_price'] * (int)$order->fields['products_quantity'];
                $order->MoveNext();
            }
        }
    }
    
    // Get product IDs from listing or search result pages
    if (empty($ecomm_prodid) && !empty($listing_sql)) {
        $products = $db->Execute($listing_sql);
        while (!$products->EOF) {
            $ecomm_prodid[] = $product_identifier === 'id' ? (string)(int)$products->fields['products_id'] : (string)$products->fields['products_model'];
            $products->MoveNext();
        }
    }
    
    // Map pages to page types and corresponding event names
    $page_map = [
        'search_result' => ['page_type' => 'searchresults', 'event_name' => 'searchresults'],
        'index' => ['page_type' => 'category', 'event_name' => 'category'],
        'product_info' => ['page_type' => 'product', 'event_name' => 'product'],
        'shopping_cart' => ['page_type' => 'cart', 'event_name' => 'cart'],
        'checkout_success' => ['page_type' => 'purchase', 'event_name' => 'purchase'],
    ];
    
    if ($this_is_home_page) {
        $ecomm_pagetype = 'home';
    } 	elseif (array_key_exists($current_page_base, $page_map)) {
    		$ecomm_pagetype = isset($page_map[$current_page_base]) ? $page_map[$current_page_base]['page_type'] : 'other';
    }
    
    // Prepare tag parameters
    $tag_params = [
        'ecomm_prodid' => $ecomm_prodid,
        'ecomm_pagetype' => (string)$ecomm_pagetype,
        'ecomm_totalvalue' => !empty($ecomm_totalvalue) ? round((float)$ecomm_totalvalue, 2) : 0,
    ];
    
    // Get the correct event name based on $ecomm_pagetype
    $event_name = isset($page_map[$current_page_base]) ? $page_map[$current_page_base]['event_name'] : 'other';
    
    ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo htmlspecialchars($ga_conversion_id, ENT_QUOTES, 'UTF-8'); ?>"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', '<?php echo htmlspecialchars($ga_conversion_id, ENT_QUOTES, 'UTF-8'); ?>');
      
      // Remarketing tag setup
      gtag('event', '<?php echo htmlspecialchars($event_name, ENT_QUOTES, 'UTF-8'); ?>', <?php echo json_encode($tag_params, JSON_NUMERIC_CHECK); ?>);
    </script>


    I did some revisions now it seems to be firing the mapping pages to page type.

  2. #32
    Join Date
    Jan 2015
    Posts
    462
    Plugin Contributions
    0

    Default Re: Google Dynamic Remarketing Mod

    I been having some issue with this script. I think finally got it to fire correctly.
    If anything else should arise i will post.


    Code:
    <?php
    // Google Remarketing
    // Dynamic Remarketing tracking code
    // For Zen-Cart 1.5
    // Author: Stef van Dijk - vandijkstef.nl
    
    // This module is delivered as is, without any warranty
    // Feel free to use and edit this module
    // Licenced: GPL - http://www.gnu.org/licenses/licenses.html#GPL
    
    // NOTE: This script only handles products identified by ID or by model-number. 
    // It does NOT differentiate products by attribute, nor by other properties such as custom fields like SKU etc
    
    
    // Google conversion ID -- Replace AW-xxxxxxxxxx with your own ID received from Google:
    $ga_conversion_id = 'AW-xxxxxxxxx';
    
    $product_identifier = 'id'; // can be 'id' or 'model', for the store's product_id or product_model_number respectively.
    
    
    
    
    // PUT YOUR CUSTOMIZATIONS ABOVE. That's all.
    
    $pages_to_ignore = array(
     		'account', 'account_edit', 'account_history', 'account_history_info', 'account_newsletters',
        'account_notifications', 'account_password', 'address_book', 'address_book_process',
        'advanced_search', 'ask_a_question', 'checkout_confirmation', 'checkout_payment',
        'checkout_payment_address', 'checkout_process', 'checkout_shipping', 'checkout_shipping_address',
        'cookie_usage', 'create_account', 'create_account_success', 'customers_authorization',
        'discount_coupon', 'down_for_maintenance', 'download', 'download_time_out', 'gv_redeem',
        'gv_send', 'info_shopping_cart', 'login', 'logoff', 'page_2', 'page_3', 'page_4',
        'page_not_found', 'password_forgotten', 'payer_auth_auth', 'payer_auth_frame',
        'payer_auth_verifier', 'popup_attributes_qty_prices', 'popup_coupon_help', 'popup_cvv_help',
        'popup_image', 'popup_image_additional', 'popup_search_help', 'popup_shipping_estimator',
        'product_reviews_write', 'redirect', 'shippinginfo', 'ssl_check', 'time_out', 'unsubscribe','search', 'checkout_one','checkout_one_confirmation','site_map','contact_us',
    	  
    );
    if (in_array($current_page_base, $pages_to_ignore)) return;
    
    
    // DO NOT CHANGE BELOW
    if (empty($ga_conversion_id) || $ga_conversion_id === '123456789') return;
    // DO NOT CHANGE BELOW!
    if ($ga_conversion_id === 'AW-xxxxxxxxxx') {
        if (defined('AW-xxxxxxxxx') && !empty(AW-xxxxxxxxx)) {
            $ga_conversion_id = AW-xxxxxxxxxx;
        } else {
            return;
        }
    }
    
    // Product_ids
    $ecomm_prodid = array();
    // Var to save value
    $ecomm_totalvalue = 0;
    
    
    // If on a product page, get the product id
    if ($product_identifier === 'id' && !empty($_GET['products_id'])) {
    	array_push($ecomm_prodid, (string)(int)$_GET['products_id']);
    } elseif ($product_identifier === 'model' && !empty($products_model)) {
        array_push($ecomm_prodid, (string)$products_model);
    }
    
    // If on the shopping_cart page, add the cart's items to the array
    if ($current_page_base === 'shopping_cart') {
    	$cart_products = $_SESSION['cart']->get_products();
        foreach ($cart_products as $the_product) {
            if ($product_identifier === 'id') {
                array_push($ecomm_prodid, (string)(int)$the_product['id']);
            } elseif ($product_identifier === 'model') {
                array_push($ecomm_prodid, (string)$the_product['model']);
            }
       	}
    }
    // If this is checkout_success (completed a purchase), we set the products so the targeting can be marked as sold
    if ($current_page_base === 'checkout_success') {
        if ($product_identifier === 'id' && !empty($order_summary['products_ordered_ids'])) {
            $data = $order_summary['products_ordered_ids'];
        } elseif ($product_identifier === 'model' && !empty($order_summary['products_ordered_models'])) {
            $data = $order_summary['products_ordered_models'];
        } else {
            // this should never happen unless the customer is refreshing the checkout-success page
            // but we don't want to double-track conversions, so we exit here in that case
            return;
        }
        if (!empty($data)) {
            foreach (explode('|', $data) as $the_product) {
                array_push($ecomm_prodid, (string)$the_product);
            }
        }
    }
    
    // Product Listing (category) pages, and search results pages
    if (empty($ecomm_prodid) && !empty($listing_sql)) {
        if (!empty($listing)) {
            $data = $listing;
        } else if ($current_page_base === 'advanced_search_result' && !empty($result)) {
            $data = $result;
        }
        if (!empty($data)) {
            foreach ($data as $the_product) {
                if ($product_identifier === 'id') {
                    array_push($ecomm_prodid, (string)(int)$the_product['products_id']);
                } elseif ($product_identifier === 'model' && !empty($the_product['products_model'])) {
                    array_push($ecomm_prodid, (string)$the_product['products_model']);
                }
            }
        }
    }
    
    // Map pages to page types. 
    // Possible types: home, searchresults, category, product, cart, purchase, other. 
    // Fallback/default if not in this list = other
    $page_map = array(
        'advanced_search_result' => 'searchresults',
        'index' => 'category',
        'product_info' => 'product',
        'product_free_shipping_info' => 'product',
        'document_product_info' => 'product',
        'product_music_info' => 'product',
        'shopping_cart' => 'cart',
        'checkout_success' => 'purchase',
    );
    
    // Set the pagetype to specific page, otherwise "other" will be pushed
    if ($this_is_home_page) {
    	$ecomm_pagetype = 'home';
    } elseif (array_key_exists($current_page_base, $page_map)) {
    	$ecomm_pagetype = $page_map[$current_page_base];
    } else {
        $ecomm_pagetype = 'other';
    }
    
    // Get the total value - Value stays empty if page is not product or cart/payment
    if ($ecomm_pagetype === 'cart' || $ecomm_pagetype === 'purchase') {
    	$ecomm_totalvalue = $_SESSION['cart']->show_total();
    } elseif ($ecomm_pagetype === 'product') {
    	$product_price = zen_get_products_display_price((int)$_GET['products_id']);
    	$ecomm_totalvalue = preg_replace('/[^\d,.]/', '', $product_price);
    }
    
     $tag_params = array(
        'ecomm_prodid' => $ecomm_prodid,
        'ecomm_pagetype' => (string)$ecomm_pagetype,
        'ecomm_totalvalue' => !empty($ecomm_totalvalue) ? round($ecomm_totalvalue, 2) : 0,
     );
    
    // Variable block for Remarketing tag
    echo '<script title="RemarketingData">
    var google_tag_params = ' . json_encode($tag_params) . ';
    </script>';
    
    // Remarketing Tag
    echo ' <script title="RemarketingTag">
    /* <![CDATA[ */ 
    var google_conversion_id = "' . $ga_conversion_id . '";
    var google_custom_params = window.google_tag_params;
    var google_remarketing_only = true;
    /* ]]> */ 
    </script>
    <script src="//www.googleadservices.com/pagead/conversion.js"></script>
    <noscript><div style="display:inline;">
      <img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/' . $ga_conversion_id . '/?value=0&amp;guid=ON&amp;script=0">
    </div></noscript>';

 

 
Page 4 of 4 FirstFirst ... 234

Similar Threads

  1. v150 Support Thread for Google reCAPTCHA plugin
    By David Allen in forum All Other Contributions/Addons
    Replies: 665
    Last Post: 18 Jan 2025, 10:28 AM
  2. v150 Google Merchant Center Feeder for ZC v1.5.x [Support Thread]
    By DivaVocals in forum Addon Admin Tools
    Replies: 504
    Last Post: 19 Nov 2024, 03:50 PM
  3. Dynamic Filter [Support Thread]
    By davowave in forum Addon Sideboxes
    Replies: 807
    Last Post: 13 Dec 2023, 05:58 AM
  4. Welcome, Google Searcher [Support Thread]
    By swguy in forum All Other Contributions/Addons
    Replies: 24
    Last Post: 8 Jan 2017, 04:47 PM
  5. Google Base Feeder Support Thread [OLD]
    By numinix in forum All Other Contributions/Addons
    Replies: 3562
    Last Post: 2 Apr 2012, 06:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR