Answers.Body: <p>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 <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">custom plugin</a> that listens for the <code>commerce_orders.onOrderComplete</code> <a href="https://craftcommerce.com/docs/events-reference#commerce_orders.onordercomplete" rel="nofollow noreferrer">event</a>:</p>
  
  <pre><code>&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);
          }
      }
  }
  </code></pre>
  
  <p>Customer order page template:</p>
  
  <pre><code>{% if craft.session.getFlash('successfulOrderPlaced') %}
  // Add Google Analytics here
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-27T17:39:06.160Z
Answers.Text: Thank you David, thats kind of a solution i want. Do i need to write a plugin for that? I don't know how to set that variable.
Answers.UserDisplayName: orionlogic
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-29T13:15:42.357Z
Answers.Text: No problem. Yeah, you're best off using a plugin for this. I've just added some sample plugin code for you.
Answers.UserDisplayName: David Teal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-27T14:21:11.833Z
Answers.LastActivityDate: 2017-04-29T13:13:20.737Z
Answers.LastEditDate: 2017-04-29T13:13:20.737Z
Answers.UserDisplayName: David Teal
Answers.Body: <p>If the user is logged in, you could store some sort of value in the user account to a field (lightswitch, perhaps?) you define that the order has been completed, so you could then insert the tracking code only if that field has not been already turned on. But this would require having to submit some sort of form in the background with AJAX to toggle the switch in their account, and might be tricky to handle if there are multiple orders etc.</p>
  
  <p>Perhaps a better, or at least alternate option is to set a cookie once the analytics code has been called once, and then check for the cookie on future page loads, hiding the analytics if it exists. You could do this pretty easily with this plugin: <a href="https://github.com/nystudio107/cookies" rel="nofollow noreferrer">https://github.com/nystudio107/cookies</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-27T13:20:40.560Z
Answers.LastActivityDate: 2017-04-27T13:20:40.560Z
Answers.UserDisplayName: Mike Pierce
Body: <p>Here is my scenario;</p>
  
  <p>When a successful transaction occur, payment form page redirects to customer order page which have the orderId in url like <code>/orders?number=1234</code></p>
  
  <p>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.</p>
  
  <p>What kind of ways should i follow? Can i do this from server, Craft side?</p>
  
  <p>Thanks.</p>
  
Tags: <plugin-craftcommerce>
Title: How to trigger code once in a thank you page?
ThreadText: # How to trigger code once in a thank you page?
  
  (Asked by: username_0 on 2017-04-27)
  
  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.
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-04-27. Score: 3 | Accepted answer
  
  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](https://craftcms.com/docs/plugins/introduction) that listens for the `commerce_orders.onOrderComplete` [event](https://craftcommerce.com/docs/events-reference#commerce_orders.onordercomplete):
  
  ```
  <?php
  namespace Craft;
  
  class FireAnalyticsPlugin extends BasePlugin
  {
      function init()
      {
          craft()->on('commerce_orders.onOrderComplete', function($event) 
          {
              craft()->userSession->setFlash('successfulOrderPlaced', true);
          }
      }
  }
  
  ```
  
  Customer order page template:
  
  ```
  {% if craft.session.getFlash('successfulOrderPlaced') %}
  // Add Google Analytics here
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you username_1, thats kind of a solution i want. Do i need to write a plugin for that? I don't know how to set that variable.  
  > **username_1**: No problem. Yeah, you're best off using a plugin for this. I've just added some sample plugin code for you.  
  
  ---
  
  ### Answer by username_2 on 2017-04-27. Score: 2
  
  If the user is logged in, you could store some sort of value in the user account to a field (lightswitch, perhaps?) you define that the order has been completed, so you could then insert the tracking code only if that field has not been already turned on. But this would require having to submit some sort of form in the background with AJAX to toggle the switch in their account, and might be tricky to handle if there are multiple orders etc.
  
  Perhaps a better, or at least alternate option is to set a cookie once the analytics code has been called once, and then check for the cookie on future page loads, hiding the analytics if it exists. You could do this pretty easily with this plugin: <https://github.com/nystudio107/cookies>
  
  
  ---
  
  