Magento2: Google analytics pageview being triggered twice

Created on 13 Nov 2017  路  14Comments  路  Source: magento/magento2


Preconditions


  1. Set Analytics code in admin
  2. Load any page

Steps to reproduce

  1. None

Expected result

  1. pageview is triggered once

Actual result

  1. pageview is triggered twice


It looks like a problem in the GoogleAnalytics/view/frontend/web/js/google-analytics.js file, line 54 on, which calls ga('send', 'pageview'... twice:

`

        ga('send', 'pageview' + config.pageTrackingData.optPageUrl);

        // Process orders data
        if (config.ordersTrackingData) {
            ga('require', 'ec', 'ec.js');

            //Set currency code
            ga('set', 'currencyCode', config.ordersTrackingData.currency);

            // Collect product data for GA
            if (config.ordersTrackingData.products) {
                $.each(config.ordersTrackingData.products, function (index, value) {
                    ga('ec:addProduct', value);
                });
            }

            // Collect orders data for GA
            if (config.ordersTrackingData.orders) {
                $.each(config.ordersTrackingData.orders, function (index, value) {
                    ga('ec:setAction', 'purchase', value);
                });
            }

            ga('send', 'pageview');`
Fixed in 2.2.x Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.2.x Reproduced on 2.3.x

Most helpful comment

I think that fix is not fully right because config.ordersTrackingData on the Success page is an object, so we can't check the 'length' property for this

All 14 comments

I can confirm the same issue on 2.2.0.

I changed as below and it seems to resolve the specific issue, it may have other consequences that I'm not yet aware of. At least with the Google Tag Assistant recording, I don't get the duplicate hits issue any more.

Step 1. : Change the GoogleAnalytics/view/frontend/web/js/google-analytics.js file

        /**
         * Removed this line
         *  ga('send', 'pageview' + config.pageTrackingData.optPageUrl)
         */
        // Process orders data
        if (config.ordersTrackingData) {
            ga('require', 'ec', 'ec.js');

            // Collect product data for GA
            if (config.ordersTrackingData.products) {
                $.each(config.ordersTrackingData.products, function (index, value) {
                    ga('ec:addProduct', value);
                });
            }
            // Collect orders data for GA
            if (config.ordersTrackingData.orders) {
                $.each(config.ordersTrackingData.orders, function (index, value) {
                    ga('ec:setAction', 'purchase', value);
                });
            }

            ga('send', 'pageview');
        } else {
            // added it here instead
            ga('send', 'pageview' + config.pageTrackingData.optPageUrl);
        }

Step 2: Remove deployed content and deploy it again, resolve any permission issues depending on your installation and language issues as well
I guess those issues are pretty unique to me... but in principle rm -R pub/static/* and deploy static content again.

The error message in Google tag assistant recording, Google analytics report-tab prior to applying above:

This hit was preceded by an identical pageview hit. It's likely that this hit is a duplicate. If you have consecutive duplicate pageview hits, your bounce rate and pageview numbers could be inaccurate.

I had made exactly the same change to mine as KarlComSe, and it resolve the issue from the pageview point of view.

I've not looked more, but it does seem to be failing to track ecommerce though, with sales values not showing.

@alexhadley I didn't test conversion tracking / sales tracking with the tag manager recording yesterday, but can see several orders from yesterday in Google Analytics. Those orders were made after the change to the .js-file. Hence, my conclusion is that above changes still tracks transactions.

@alexhadley, thank you for your report.
We've created internal ticket(s) MAGETWO-84155 to track progress on the issue.

@magento-engcom-team, is there any update on this fix? We just moved to magento 2.2 and are seeing the same issue as above. Pageviews are being tracked twice which is causing bounce rates and pages per session to be incorrect. Ecommerce Tracking of transactional data is coming through correctly.

Working on fixing this issue #MM18IN

@bhargavmehta thank you for joining. Please accept team invitation here and self-assign the issue.

Hi @alexhadley. Thank you for your report.
The issue has been fixed in magento/magento2#13034 by @bhargavmehta in 2.2-develop branch
Related commit(s):

  • 149d08fbae7a0c747e816fb47752581486c27677
  • e851948a93f63887f053559802b6de1348db4a56

The fix will be available with the upcoming patch release.

Hi @alexhadley. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#1351 by @magento-engcom-team in 2.3-develop branch
Related commit(s):

  • 70dd5a3fa17e1ee1cdfaf7f0defd9276989ebbd0
  • 54bc3a1dca861e11e46e12f5a15578e0cb7ceb6d
  • 3debd3f87e27e23949b81cb2d7e881b5a269608c
  • 72c496566bf0d2ff1ced0401514faa5cbaccc0f8
  • e60ecdfeabe8b8839cf52fd5ba7fefcbb0b9bdc3
  • 8703b3da3dbe28f71c1daeaba08bbe44887f0733
  • bd88ed8cc4e1f98b791bd7ca8bed45c29b276ee1
  • 1c70b18a339fddf8a51a476a5a251aabef951350
  • 5fd7481d2cf8b298369837508a16a23ab4f49850
  • 7c615065b36cdafa95d752a891c09dd4ab1a4dfa
  • 2653b8ae55625637a6836d305451e292f881bd6a
  • 917a03f89f98454cb2ecde524dc8557d7fdcc135
  • eca73d0abd1ca6e110c364ef380adab86ab3cc1a
  • 26f3109842ce89096a49b9c122af0bae6b672ba2
  • c223484a75e38fdcece09d4586342528f06c6383
  • 3b90ad71d791ba9cf8e81cc204fd306f41473a18
  • ad317c25039de70c1a976e1139c57730899eb114
  • 53e721ccc06f1ade05132690715575bd7d47d6b7
  • 6114120c0a921b342871448dfb6c47bf34a65cdc
  • 63b483925c18304a32441f66b0db8a9c70995c73

The fix will be available with the upcoming 2.3.0 release.

I think that fix is not fully right because config.ordersTrackingData on the Success page is an object, so we can't check the 'length' property for this

Conversion doesnt work here too. Ravinsky is right I guess.

You have to modify the ga.phtml file to get the ecommerce tracking because as @ravinsky said you can't use .lenght on an object so you have to keep the old statement in the js file too:

<?php
/**
 * Copyright 漏 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var $block \Magento\GoogleAnalytics\Block\Ga */ ?>
<?php $accountId = $block->getConfig(\Magento\GoogleAnalytics\Helper\Data::XML_PATH_ACCOUNT) ?>
<?php $orderTrackingData = $block->getOrdersTrackingData() ?>
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/x-magento-init">
{
    "*": {
        "Magento_GoogleAnalytics/js/google-analytics": {
            "isCookieRestrictionModeEnabled": <?= (int)$block->isCookieRestrictionModeEnabled() ?>,
            "currentWebsite": <?= (int)$block->getCurrentWebsiteId() ?>,
            "cookieName": "<?= /* @escapeNotVerified */ \Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE ?>",
            <?php if (!empty($orderTrackingData)): ?>
            "ordersTrackingData": <?= /* @escapeNotVerified */ json_encode($orderTrackingData) ?>,
            <?php else: ?>
            "ordersTrackingData": false,
            <?php endif; ?>
            "pageTrackingData": <?= /* @escapeNotVerified */ json_encode($block->getPageTrackingData($accountId)) ?>
        }
    }
}
</script>
<!-- END GOOGLE ANALYTICS CODE -->

Hi @alexhadley. Thank you for your report.
The issue has been fixed in magento/magento2#15765 by @torhoehn in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.6 release.

Hi @alexhadley. Thank you for your report.
The issue has been fixed in magento/magento2#15847 by @torhoehn in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

Was this page helpful?
0 / 5 - 0 ratings