How to trigger code once in a thank you page? Here is my scenario;

When a successful transaction occur, payment form page redirects to customer order page which have the orderId in url like /orders?number=1234

In this page i send ecommerce tag into Google Analytics.However this should be send only once, in first visit. So when the customer returns order page and look again, this code should not be fired.

What kind of ways should i follow? Can i do this from server, Craft side?

Thanks.
We do this by setting a flash message server side and then checking for this in the template. Flash messages are only shown once so no need to manually delete/unset. The best way to do this would be using a custom plugin that listens for the commerce_orders.onOrderComplete event:

&lt;?php
namespace Craft;

class FireAnalyticsPlugin extends BasePlugin
{
    function init()
    {
        craft()-&gt;on('commerce_orders.onOrderComplete', function($event) 
        {
            craft()-&gt;userSession-&gt;setFlash('successfulOrderPlaced', true);
        }
    }
}


Customer order page template:

{% if craft.session.getFlash('successfulOrderPlaced') %}
// Add Google Analytics here
{% endif %}