<%= yield :analytics %>

<%= analytics do %>
  <%# intercom displays a new message button in overview of messages. For now we don't want our users to initiate conversations %>
  <%- unless analytics_intercom_widget? %>
    <style type="text/css">
      #intercom-container .intercom-sheet-footer {
        display: none !important;
      }
    </style>
  <% end %>

  <script type="text/javascript">
    !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src="https://cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.0.1";
      analytics.load("<%= segment.write_key %>");

      <%- if analytics_intercom_widget? -%>
        window.intercomSettings = { widget: { activator: '#IntercomDefaultWidget'}};
      <%- end -%>

      analytics.ready(function(){
        try {
          Munchkin.init(window.analytics._integrations.Marketo.options.accountId, { asyncOnly: true, disableClickDelay: true });
        } catch (e) { console.error(e) }
      });
    }}();
  </script>

  <script>
    <%- while event = analytics_session.shift -%>
    <%- name, *args = event -%>
    analytics[<%= json name %>].apply(analytics, <%= json args %>);
    <%- end -%>

    analytics.identify(<%= json current_user.try(:id) %>, <%= json analytics_identity_data %>);
    analytics.page(<%= json @analytics_page %>);

    <%- if current_user && current_user.account.users.but_3scaleadmin.size > 1  -%>
    analytics.group(<%= current_user.account.id %>, {
      name: <%= json current_user.account.org_name %>
    });
    <%- end -%>
  </script>
<% end %>
