Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save danieliser/c24b1eaa91d405d8351f3c0dc1f7883d to your computer and use it in GitHub Desktop.

Select an option

Save danieliser/c24b1eaa91d405d8351f3c0dc1f7883d to your computer and use it in GitHub Desktop.

MonsterInsights eCommerce Addon: Duplicate Script Tag Bug

Plugin: MonsterInsights eCommerce (ga-ecommerce) v8.6.0 Core: MonsterInsights Pro (google-analytics-premium) v10.0.1

Issue

ga-ecommerce/includes/providers/ecommerce-integration.php line 129 outputs a full <script> tag inside the monsterinsights_frontend_tracking_gtag_after_pageview action callback:

// ecommerce-integration.php line 93 - hooks into the action
add_action( 'monsterinsights_frontend_tracking_gtag_after_pageview', array( $this, 'print_dual_tracking_js' ), 11, 1 );

// ecommerce-integration.php lines 129-202 - outputs its own <script> wrapper
<script<?php echo $attr_string; ?>>
    window.MonsterInsightsDualTracker.helpers.mapProductItem = function (uaItem) {
        // ... DualTracker code ...
    };
</script>

However, google-analytics-premium/includes/frontend/tracking/class-tracking-gtag.php lines 480-513 captures all callbacks on that action with ob_start() and wraps the output in its own <script> tag:

// class-tracking-gtag.php lines 486-510
ob_start();
call_user_func( $callback_data['function'] );
$callback_output = ob_get_clean();

if ( ! empty( trim( $callback_output ) ) ) {
    ?>
    <script<?php echo $attr_string; ?>>
        <?php echo $callback_output; ?>
    </script>
    <?php
}

Result: Double Script Tags

This produces the following broken HTML:

</script>
<script data-cfasync="false" data-wpfc-render="false">   <!-- from class-tracking-gtag.php line 507 -->
<script data-cfasync="false" data-wpfc-render="false">   <!-- from ecommerce-integration.php line 129 -->
window.MonsterInsightsDualTracker.helpers.mapProductItem = function (uaItem) {

Browser throws: Uncaught SyntaxError: Unexpected token '<' at the second <script> tag.

Screenshot

The duplicate tags visible in View Source:

Line 275:  </script>
Line 276:  <script data-cfasync="false" data-wpfc-render="false">
Line 277:  <script data-cfasync="false" data-wpfc-render="false">
Line 278:  window.MonsterInsightsDualTracker.helpers.mapProductItem = function (uaItem) {

Fix

Remove the <script> and </script> wrapper from ecommerce-integration.php lines 129 and 202. The callback should output raw JS only, since the parent callback loop in class-tracking-gtag.php already wraps it in a script tag.

Workaround

Commenting out line 129 (script open tag) and line 202 (script close tag) of ecommerce-integration.php resolves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment