SynXis: Booking on Your Own Domain

Use this setup if guests book on your own domain, such as reserve.yourhotel.com. Complete the SynXis prerequisites first.

Step One: Add click tracking to your website

Add this script to every page of your website. Either place it just before the closing </body> tag, or add it through Google Tag Manager as a Custom HTML tag that fires on every page view. Replace [domain] with your website's root domain, without www (for example yourhotel.com).

<script
  async
  src="https://static.shopmy.us/Affiliates/sms_aff_clicktrack.js?domain=[domain]&expires=30"
></script>

Step Two: Add click tracking to your booking engine

If your booking site is different than your marketing site, then complete this step otherwise skip to step 3.

  1. In Google Tag Manager, open the container connected to your SynXis booking engine
  2. Title the tag "ShopMy Click Tracking"
  3. Set the Tag Type to Custom HTML
  4. Set the HTML to the script below, using the same [domain] as in Step One
  5. Set the Triggering field to fire on a "Page View" on "All Pages"
  6. Press Save
<script
  async
  src="https://static.shopmy.us/Affiliates/sms_aff_clicktrack.js?domain=[domain]&expires=30"
></script>

Using the same [domain] lets your website and booking engine share the click.

Step Three: Add the booking confirmation script

  1. In the same container, create a new tag titled "ShopMy Booking Confirmation"
  2. Set the Tag Type to Custom HTML
  3. Paste the code below without editing it
  4. Set the tag to trigger on order confirmation
  5. Press Save
(function() {
  var POLL_INTERVAL_MS = 1000;
  var POLL_TIMEOUT_MS = 30000;
  var reported = false;
  var requestInFlight = false;

  function toIsoDate(raw) {
    if (!raw) return null;
    var str = String(raw);
    if (/^\d{4}-\d{2}-\d{2}/.test(str)) return str.slice(0, 10);
    var mdy = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
    if (mdy) return mdy[3] + '-' + ('0' + mdy[1]).slice(-2) + '-' + ('0' + mdy[2]).slice(-2);
    return null;
  }

  function reportOrderIfConfirmed() {
    if (reported || requestInFlight) return;

    var globalTracking = window._globalTracking || {};
    var cartReservation = (globalTracking.Cart && globalTracking.Cart[0]) || {};
    var confirmedReservation = (globalTracking.ConfirmedReservations && globalTracking.ConfirmedReservations[0]) || {};
    var reservation = cartReservation.ConfirmNo ? cartReservation : confirmedReservation;
    var hotelId = String(globalTracking.HOTEL_ID || (window.location.search.match(/[?&]hotel=([^&]+)/i) || [])[1] || '');

    var orderAmount = globalTracking.TotalCost || reservation.TotalCost;
    if (!hotelId || !reservation.ConfirmNo || !orderAmount) return;

    var cookies = {};
    var cookiesList = document.cookie.split(';');
    for (var i = 0; i < cookiesList.length; i++) {
      if (cookiesList[i]) {
        cookies[(cookiesList[i].split('=')[0] || '').trim()] = (cookiesList[i].split('=')[1] || '').trim();
      }
    }
    var clickId = cookies.sms_click_id;
    if (!clickId) {
      try { clickId = sessionStorage.getItem('sms_click_id'); } catch (_e) { /* blocked */ } // eslint-disable-line no-unused-vars
    }

    var payload = {
      orderAmount: orderAmount,
      orderId: reservation.ConfirmNo,
      currency: reservation.CurrCode || globalTracking.CurrCode || 'USD',
      is_returning_customer: false,
      page_url: window.location.href,
      propertyId: hotelId,
      returnWindowClosesAt: toIsoDate(reservation.ArrivalDt || globalTracking.ArrivalDt)
    };
    if (clickId) payload.clickId = clickId;

    requestInFlight = true;
    fetch('https://api.shopmy.us/api/order_confirmation', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(payload)
    })
      .then(function(r) {
        reported = true;
        if (!r.ok) throw new Error('Invalid order request sent');
        return r.json();
      })
      .then(function() {
        var now = new Date();
        document.cookie = 'sms_click_id=;expires=' + now.toUTCString() + ';path=/;';
        document.cookie = 'sms_click_time=;expires=' + now.toUTCString() + ';path=/;';
        // The clicktrack scopes the cookies to the root domain, which a host-only clear from this subdomain misses.
        var hostParts = window.location.hostname.split('.');
        for (var j = 0; j < hostParts.length - 1; j++) {
          var cookieDomain = hostParts.slice(j).join('.');
          document.cookie = 'sms_click_id=;domain=' + cookieDomain + ';expires=' + now.toUTCString() + ';path=/;';
          document.cookie = 'sms_click_time=;domain=' + cookieDomain + ';expires=' + now.toUTCString() + ';path=/;';
        }
        try { sessionStorage.removeItem('sms_click_id'); } catch (_e) { /* blocked */ } // eslint-disable-line no-unused-vars
      })
      .catch(function(e) {
        console.warn('ShopMy: order confirmation failed', e);
      })
      .then(function() {
        requestInFlight = false;
      });
  }

  try {
    reportOrderIfConfirmed();
    if (!reported) {
      var pollStartedAt = new Date().getTime();
      var poll = setInterval(function() {
        try {
          if (new Date().getTime() - pollStartedAt >= POLL_TIMEOUT_MS) return clearInterval(poll);
          reportOrderIfConfirmed();
          if (reported) clearInterval(poll);
        } catch (pollError) {
          clearInterval(poll);
          console.warn('We had an issue with the ShopMy order confirmation', pollError);
        }
      }, POLL_INTERVAL_MS);
    }
  } catch (error) {
    console.warn('We had an issue with the ShopMy order confirmation', error);
  }
})();

Step Four: Publish changes

Whenever you change your Google Tag Manager tags, press Publish at the top-right of the page in each container you changed.