Learn business growth with Google Analytics 4 Forums Google Analytics 4 How can I exclude purchases with unassigned transaction IDs in GA4?

  • How can I exclude purchases with unassigned transaction IDs in GA4?

    Posted by Ella on 15 February 2023 at 12:53 pm

    Hey there, I’ve been messing around with a GA4 account for a mate’s Shopify store lately. I’ve hit a bit of a roadblock where I’m trying to stop sending purchases without transaction IDs to analytics/revenue count (aka when the transaction ID is (not set)). Just wondering if there’s a chance the solution could be brewing in the liquid on the checkout page? Here’s what the current purchase event looks like:

    window.dataLayer = window.dataLayer || [];
    dataLayer.push({ ecommerce: null });
    {% if first_time_accessed %}
    dataLayer.push({
      event: "purchase",
      ecommerce: {
          transaction_id: "{{ order.order_number }}",
          value: {{ total_price | times: 0.01 }},
          tax: {{ tax_price | times: 0.01 }},
          shipping: {{ shipping_price | times: 0.01 }},
          currency: "{{ order.currency }}",
          items: [
           {% for line_item in line_items %}{
            item_id: "{{ line_item.product_id }}",
            item_name: "{{ line_item.title | remove: "'" | remove: '"' }}",
            currency: "{{ order.currency }}",
            price: {{ line_item.final_price | times: 0.01 }},
            quantity: {{ line_item.quantity }}
          },{% endfor %}
    ]
      }
    });
    {% endif %}
    

    Gotta say, any pointers here would be super appreciated!

    Jacob replied 12 months ago 3 Members · 2 Replies
  • 2 Replies
  • Mason

    Member
    12 June 2023 at 9:34 am

    From the code snippet you provided, it seems there could be instances where the orders do not have an associated order number, possibly leading to your ‘not set’ issue. To resolve this, you could add an additional if-statement to your script to check if the order number is defined before pushing the purchase event to the dataLayer. You can add the condition if order.order_number or if order.order_number != "" before pushing the data to Google Analytics. This way, only transactions with a set transaction ID will be tracked. If your order_number is coming up as null in the first place, you might also need to review your order processing and data extraction to make sure all orders are properly assigned an order number.

  • Jacob

    Member
    14 June 2023 at 7:37 am

    It looks like you may need to add a conditional check to ensure that the transaction_id has a value before pushing the purchase event into the dataLayer. Using Shopify’s Liquid language, you can implement a simple check around the dataLayer.push() function that checks whether the transaction_id (or order.order_number in your code) has been set. You’d do this by wrapping the purchase event inside an if statement that checks if order.order_number is not empty. If the order.order_number is not available, the purchase event won’t be pushed to the data layer. If there are other places in the code where you’re pushing data to dataLayer, you’d also need to implement similar conditional checks. As always, after making these changes, remember to test thoroughly to make sure it works as expected and does not interfere with other functions or tracking features.

Log in to reply.