Problem with conversion tracking after purchase via przelewy24

Started by souacz53, September 25, 2024, 18:21:30 PM

Previous topic - Next topic

souacz53

Hi, I have a problem with correct tracking of conversions after a purchase using przelewy24.

In the plugin settings I can add GA Key and after each successful purchase information about the transaction is sent to Google. The problem is with placing the conversion code in the page code.

After the purchase, the code instead of remaining hidden, is displayed after the message about the correct course of the transaction. Moreover, it is not correct code, it is text, not enclosed in "<script>" tags, just simple text.

I see in the plugin's php file that the "ga_script" variable is placed right after the message about a correct transaction, but I can't correct the code so that the correct script is placed in the page source and is not visible to the page user.

Here is the code that places the function in the code (search for "ga_script"):
// success
        $order_id = JRequest::getString('on');

        if (!empty($order_id)) {
            $db = JFactory::getDBO();
            $order_id = addslashes($order_id);
            $db->setQuery('SELECT p24.*, ord.order_status, ord.virtuemart_order_id FROM ' . $this->_tablename . ' as p24 JOIN `#__virtuemart_orders` as ord using(virtuemart_order_id) WHERE p24.order_number="' . $order_id . '"');
            $payment_db = $db->loadObject();

            if(!$payment_db)
                return null;

            $modelOrder = VmModel::getModel('orders');
            $order = $modelOrder->getOrder($payment_db->virtuemart_order_id);
            if (!($method = $this->getVmPluginMethod($order['details']['BT']->virtuemart_paymentmethod_id)))
                return null;

            if ($payment_db->order_status == 'C') {
                $ga_script = '';
                if (strlen($method->key_ga)) {
                    $ga_script = $this->generateGoogleAnalyticsTrackingCode($order_id, $order, $method);
                }
                JFactory::getApplication()->enqueueMessage(JText::_('PLG_PRZLELEWY24_SUCCESS') . $ga_script);
            } elseif ($payment_db->order_status == 'X') {
                JError::raiseWarning(100, JText::_('PLG_PRZLELEWY24_FAIL'));
                JFactory::getApplication()->enqueueMessage(JText::_('PLG_PRZLELEWY24_FAIL'));
            } else {
                $this->changeOrderStatus($payment_db->virtuemart_order_id, 'I', JText::_('PLG_PRZLELEWY24_WAITING'), $method->mail);
                JFactory::getApplication()->enqueueMessage(JText::_('PLG_PRZLELEWY24_WAITING'));
            }
        }

And this is the function definition (search for "ga_script"):
private function generateGoogleAnalyticsTrackingCode($order_id, $order, $method)
    {
        $categoryModel = VmModel::getModel('Category');
        $currency = $this->getCurrencyCodeById($order['details']['BT']->order_currency, $method);
        $shopName = JFactory::getConfig()->get('sitename');
        $total = $method->ga_gross_price == 1 ? $order['details']['BT']->order_total : $order['details']['BT']->order_subtotal;
        $transaction = <<<HTML
                                ga('ecommerce:addTransaction', {
                                  'id': '{$order_id}',
                                  'affiliation': '{$shopName}',
                                  'revenue': '{$total}',
                                  'shipping': '{$order['details']['BT']->order_shipment}',
                                  'tax': '{$order['details']['BT']->order_tax}',
                                  'currency': '{$currency}'
                                });
HTML;

        $products = $order['items'];
        $items = '';
        if (is_array($products)) {
            foreach ($products as $product) {
                $category = $categoryModel->getCategory($product->virtuemart_category_id);
                $productTotal = $method->ga_gross_price == 1 ? $product->product_final_price : $product->product_priceWithoutTax;
                $items .= <<<HTML
                                    ga('ecommerce:addItem', {
                                        'id': '{$order_id}',
                                        'name': '{$product->order_item_name}',
                                        'sku': '{$product->order_item_sku}',
                                        'category': '{$category->category_name}',
                                        'price': '{$productTotal}',
                                        'quantity': '{$product->product_quantity}',
                                        'currency': '{$currency}'
                                      });
HTML;
            }
        }

        $ga_script = <<<HTML
                            <script>
                              (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                              (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                              m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                              })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

                              ga('create', '{$method->key_ga}', 'auto');
                              ga('require', 'ecommerce');
                              {$transaction}
                              {$items}
                              ga('ecommerce:send');
                            </script>
HTML;

        return $ga_script;
    }

I haven't changed anything in this plugin, this is the original state. The plugin's creator has dematerialized and I have no way of contacting him. Hence the request for help from the VM community.


Jumbo!

The codes are incorrect. You can not add scripts using the enqueueMessage function. Even the "ga" tracking scripts are obsolete and no longer valid. You may refer to the official Google documentation for the updated gtag.js or GTM implementation - https://developers.google.com/analytics/devguides/collection/ga4/set-up-ecommerce.

Alternatively, you can use our VP Conversion Tracking plugin - https://www.virtueplanet.com/extensions/vp-conversion-tracking.

souacz53

Thank you very much for your reply. I would like to use your plugin, but the transaction summary page using prtzelewy24 is not the standard "thank you" page from VM. So will it work?

souacz53


Jumbo!

This is not our support system. Since you shared a screenshot of the plugin settings, we believe you already own it. We have dedicated support forum for the VP Conversion Tracking plugin: https://www.virtueplanet.com/forum/vp-conversion-tracking. Post your concern there, and we will gladly help you.