• Resolved norabrown212

    (@norabrown212)


    Hello, my client was noticing duplicate e-commerce transactions specifically from mobile devices. When I looked at the plugin code, I saw that this should not happen when a page is reloaded from the server. (in woocommerce.php for example).

    However, I noticed that on some mobile browsers, when a user returns to a browser tab after some time, JavaScript on the page is executed again, even though the page is not re-fetched from the server. This is what I believe was causing the duplicate transactions. I added code in frontend.php to set and check a transaction-specific cookie, and if it exists, to not resend the transaction to GA.

    		$transId = $gtm4wp_datalayer_data["transactionId"];
    		$_gtm_header_content .= '
    		if(Cookies && !Cookies.get("ga-transaction-' . $transId . '-tracked") ){	
    	' . $gtm4wp_datalayer_name . '.push(' . str_replace(
    			array( '"-~-', '-~-"' ),
    			array( "", "" ),
    			str_replace( "?", "-", $dl_json_data )
    		) . ');
    		}';
    			if($gtm4wp_datalayer_data["transactionId"]){
    				$transId = $gtm4wp_datalayer_data["transactionId"];
    				echo '<script>if(Cookies) { Cookies.set("ga-transaction-' . $transId . '-tracked", true, { expires: 14 }) }</script>';
    			}

    Would you consider adding this, or something like it, to the plugin? Essentially, check both server-side AND client-side if the transaction has already been sent to GA.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    Thanks for the suggestion, this could solve the issue for many other users who reported duplicate transactions but I was not able to recreate the issue on their sites.

    I’ve added this to the GitHub issue tracker:
    https://github.com/duracelltomi/gtm4wp/issues/118

    Thread Starter norabrown212

    (@norabrown212)

    I found I can reproduce consistently (using a very bare-boned test case) on iphone in Safari. I think the client who had the duplicate transactions may be somewhat unique because they are selling tickets, and people may leave the “order confirmation” tab open intentionally, to have their order information handy at the door, causing the dupes when they return to it.

    Thanks for the plugin!

    • This reply was modified 5 years, 7 months ago by norabrown212.

    Hello norabrown212,

    Can you please tell me where i need to add your given code. you said to add frontend.php but i do not understand which position do i need to add that code. I assume, need to add code inside this function: “gtm4wp_wp_header_begin”. Can you please confirm me. Also, do i need to add this code direct to plugin file? or is tghere any way so that i can copy frontend.php file inside my child theme and then modify? please help me about this.

    Thanks,
    Hasan

    • This reply was modified 5 years, 7 months ago by hasanraju123.
    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi Hasan,

    I am not sure what code do you refer to.
    In this thread we talked about a possible solution to prevent duplicate transactions being tracked with WooCommerce on mobile devices.

    The code of norabrown212 needs to be changed in woocommerce.php in the plugin directory of GTM4WP

    Thread Starter norabrown212

    (@norabrown212)

    Hi Hasan,

    I should note that the code above (yes, it goes in frontend.php) applied to a prior version of the plugin. I’ve just upgraded to the latest version, and have not yet added the code to the new version, nor noticed any duplicate transactions. I will post an update here if and when I do.

    Unfortunately, I don’t think there’s a way to customize a plugin with a child theme.

    Thread Starter norabrown212

    (@norabrown212)

    My client again saw duplicate transactions after I removed the patch (somewhat unintentionally, by upgrading to the latest version of the plugin). I put my patch back in to /public/frontend.php as follows:

    (NOTE this requires https://github.com/js-cookie/js-cookie which was already being loaded on the site — you can use another cookie library or use native js).

    Original lines 833-834:

    $_gtm_header_content .= '
    ' . $gtm4wp_datalayer_name . '.push(' . $gtm4wp_datalayer_json . ');';

    New:

    // if there is a transaction
    if($gtm4wp_datalayer_data["transactionId"]){
      $transId = $gtm4wp_datalayer_data["transactionId"];
      $_gtm_header_content .= '
      // only push the transaction if the transaction-specific cookie is not set
      if(typeof Cookies !== "undefined" && !Cookies.get("ga-transaction-' . $transId . '-tracked") ){
    ' . $gtm4wp_datalayer_name . '.push(' . $gtm4wp_datalayer_json . ');
      }';
    } else {
      $_gtm_header_content .= '
      ' . $gtm4wp_datalayer_name . '.push(' . $gtm4wp_datalayer_json . ');';
    }

    Original lines 873 to 879:

    if ( !gtm4wp_amp_running() ) {
      if ( $echo ) {
       echo $_gtm_header_content;
      } else {
       return $_gtm_header_content;
      }
    }

    New:

    if ( !gtm4wp_amp_running() ) {
      if ( $echo ) {
        echo $_gtm_header_content;
        // if there is a transaction, set a transaction-specific cookie
        if($gtm4wp_datalayer_data["transactionId"]){
          $transId = $gtm4wp_datalayer_data["transactionId"];
          echo '<script>if(typeof Cookies !== "undefined") { Cookies.set("ga-transaction-' . $transId . '-tracked", true, { expires: 14 }) }</script>';
        }
      } else {
       return $_gtm_header_content;
      }
    }
    • This reply was modified 5 years, 7 months ago by norabrown212.
    • This reply was modified 5 years, 7 months ago by norabrown212.
    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    Thanks for the code sample, I will use it to implement this second line of protection.

    Hello duracelltomi,

    Do you have any plan to update plugin by using code which suggested “norabrown212”? This way plugin will fix this duplicate problem and we do not need to edit plugin file.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    Yes, the mentioned protection will be added into the plugin core, I will do my best to make this happen soon.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    I have added a bit more complex protection as in many cases a frontend only solution was needed.

    More details:
    https://github.com/duracelltomi/gtm4wp/commit/34d1d05f42a6558973a73825a1ed13d4804de831

    That commit has me really excited for version 1.10’s release.

    Thank you, @duracelltomi, and I look forward to testing this and finding it resolves the problem we’ve had.

    This might be good if we can pin it in the front page?

    Thank you.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Good idea, done!

    (FTR: v1.10 still needs some testing but it is very near to be released)

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Duplicate transactions from mobile devices’ is closed to new replies.