{% if option.script_tags.custom.head is defined and option.script_tags.custom.head %}
{{ include(template(option.script_tags.custom.head))|raw }}
{% endif %}

{% if option.script_tags.gtm is defined and option.script_tags.gtm.is_enabled and option.script_tags.gtm.container_id is defined and option.script_tags.gtm.container_id %}
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','{{ option.script_tags.gtm.container_id }}');</script>
  <!-- End Google Tag Manager -->
{% endif %}

{% if option.script_tags.ga.is_enabled is defined and option.script_tags.ga.is_enabled and option.script_tags.ga.measurement_id is defined and option.script_tags.ga.measurement_id %}
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ option.script_tags.ga.measurement_id }}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', '{{ option.script_tags.ga.measurement_id }}');
</script>
{% endif %}

{% if option.script_tags.intercom.is_enabled is defined and option.script_tags.intercom.is_enabled and option.script_tags.intercom.app_id is defined and option.script_tags.intercom.app_id %}
<script>
  window.intercomSettings = {
    api_base: "https://api-iam.intercom.io",
    app_id: "{{ option.script_tags.intercom.app_id }}",

    {% if user is defined %}
    name: `{{ user.first_name }} {{ user.last_name }}`, 
    user_id: `{{ user.id }}`,
    email: `{{ user.email }}`,
    created_at: {{ user.created_at }},
    company: {
      id: `{{ workspace.id }}`,
      name: `{{ workspace.name }}`,
      created_at: {{ workspace.created_at }},
      {% if workspace.subscription %}
      plan: `{{ workspace.subscription.plan.title }}`,
      {% endif %}
    },
    companies: [
      {% for ws in user.owned_workspaces|merge(user.workspaces) %}
      {
        id: `{{ ws.id }}`,
        name: `{{ ws.name }}`,
        created_at: {{ ws.created_at }},
      },  
      {% endfor %}
    ],
    
    {% if option.script_tags.intercom.verification_is_enabled is defined and option.script_tags.intercom.verification_is_enabled and option.script_tags.intercom.secret_key is defined and option.script_tags.intercom.secret_key %}
    user_hash: "{{ hash_hmac('sha256', user.id, option.script_tags.intercom.secret_key) }}",
    {% endif %}
    {% endif %}

  };
</script>

<script>
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/{{ option.script_tags.intercom.app_id }}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
</script>
{% endif %}