{%- liquid
  assign cs_rules = app.metafields.cs_x9k2m.rules.value
  assign cs_url_map = app.metafields.cs_x9k2m.url_map.value
  assign cs_noindex = false
  assign cs_nofollow = false
  assign cs_canonical = ''
  assign cs_needs_param_js = false
  assign cs_matched_rule = ''
-%}
{%- if cs_rules -%}
{%- if cs_rules.noindex_nofollow_customer_login_page == true and template contains 'customers/login' -%}{%- assign cs_noindex = true -%}{%- assign cs_nofollow = true -%}{%- assign cs_matched_rule = 'noindex_nofollow_customer_login_page' -%}{%- endif -%}
{%- if cs_rules.noindex_nofollow_customer_account_page == true -%}{%- if template contains 'account' or request.path contains '/customer_authentication/' -%}{%- assign cs_noindex = true -%}{%- assign cs_nofollow = true -%}{%- assign cs_matched_rule = 'noindex_nofollow_customer_account_page' -%}{%- endif -%}{%- endif -%}
{%- if cs_rules.noindex_nofollow_cart_page == true and template.name == 'cart' -%}{%- assign cs_noindex = true -%}{%- assign cs_nofollow = true -%}{%- assign cs_matched_rule = 'noindex_nofollow_cart_page' -%}{%- endif -%}
{%- if cs_rules.noindex_nofollow_checkout_page == true -%}{%- if template contains 'checkout' or request.path contains '/checkouts/' -%}{%- assign cs_noindex = true -%}{%- assign cs_nofollow = true -%}{%- assign cs_matched_rule = 'noindex_nofollow_checkout_page' -%}{%- endif -%}{%- endif -%}
{%- if cs_rules.noindex_nofollow_collection_all == true and template contains 'collection' and collection.handle == 'all' -%}{%- assign cs_noindex = true -%}{%- assign cs_nofollow = true -%}{%- assign cs_matched_rule = 'noindex_nofollow_collection_all' -%}{%- endif -%}
{%- if cs_rules.noindex_tagged_collections == true and template contains 'collection' and current_tags -%}{%- assign cs_noindex = true -%}{%- assign cs_canonical = collection.url | prepend: shop.url -%}{%- assign cs_matched_rule = 'noindex_tagged_collections' -%}{%- endif -%}
{%- if cs_rules.noindex_tagged_blogs == true and template contains 'blog' and current_tags -%}{%- assign cs_noindex = true -%}{%- assign cs_canonical = blog.url | prepend: shop.url -%}{%- assign cs_matched_rule = 'noindex_tagged_blogs' -%}{%- endif -%}
{%- if cs_rules.noindex_vendor_urls == true and template contains 'collection' and collection.handle == 'vendors' -%}{%- assign cs_noindex = true -%}{%- if cs_canonical == '' -%}{%- assign cs_canonical = shop.url -%}{%- endif -%}{%- assign cs_matched_rule = 'noindex_vendor_urls' -%}{%- endif -%}
{%- if cs_rules.noindex_collection_urls_with_sort_parameters == true -%}{%- if collection.sort_by == 'manual' or collection.sort_by == 'created-ascending' or collection.sort_by == 'created-descending' or collection.sort_by == 'best-selling' or collection.sort_by == 'title-ascending' or collection.sort_by == 'title-descending' or collection.sort_by == 'price-ascending' or collection.sort_by == 'price-descending' -%}{%- assign cs_noindex = true -%}{%- if cs_canonical == '' -%}{%- assign cs_canonical = collection.url | prepend: shop.url -%}{%- endif -%}{%- assign cs_matched_rule = 'noindex_collection_urls_with_sort_parameters' -%}{%- endif -%}{%- endif -%}
{%- if cs_rules.noindex_search_urls == true and template contains 'search' -%}{%- assign cs_noindex = true -%}{%- if cs_canonical == '' -%}{%- assign cs_canonical = shop.url -%}{%- endif -%}{%- assign cs_matched_rule = 'noindex_search_urls' -%}{%- endif -%}
{%- if cs_rules.noindex_wpm_and_sandbox == true -%}{%- if request.path contains 'web-pixels-manager' or request.path contains '/wpm' or request.path contains 'sandbox' -%}{%- assign cs_noindex = true -%}{%- assign cs_matched_rule = 'noindex_wpm_and_sandbox' -%}{%- endif -%}{%- endif -%}
{%- if cs_rules.noindex_handles == true -%}{%- if cs_url_map and cs_url_map.handles -%}{%- assign cs_map_hit = cs_url_map.handles[handle] -%}{%- if cs_map_hit -%}{%- assign cs_noindex = true -%}{%- assign cs_matched_rule = 'noindex_handles' -%}{%- endif -%}{%- else -%}{%- assign cs_handle_val = cs_rules.noindex_handles_value -%}{%- if cs_handle_val and cs_handle_val != '' -%}{%- assign cs_handle_arr = cs_handle_val | split: ',' -%}{%- for cs_item in cs_handle_arr -%}{%- assign cs_item_s = cs_item | strip | remove: '/' -%}{%- if cs_item_s != '' and handle == cs_item_s -%}{%- assign cs_noindex = true -%}{%- assign cs_matched_rule = 'noindex_handles' -%}{%- break -%}{%- endif -%}{%- endfor -%}{%- endif -%}{%- endif -%}{%- endif -%}
{%- if cs_rules.noindex_specific_url == true -%}{%- if cs_url_map and cs_url_map.paths -%}{%- assign cs_path_norm = request.path | remove_last: '/' -%}{%- if cs_path_norm == '' -%}{%- assign cs_path_norm = '/' -%}{%- endif -%}{%- for cs_pe in cs_url_map.paths -%}{%- assign cs_pe_prefix = cs_pe.prefix -%}{%- if cs_pe_prefix and cs_pe_prefix != '' -%}{%- assign cs_pe_pfx = cs_pe_prefix | append: '/' -%}{%- if cs_path_norm == cs_pe_prefix or request.path contains cs_pe_pfx -%}{%- assign cs_noindex = true -%}{%- assign cs_matched_rule = 'noindex_specific_url' -%}{%- break -%}{%- endif -%}{%- endif -%}{%- endfor -%}{%- else -%}{%- assign cs_path_val = cs_rules.noindex_specific_url_value -%}{%- if cs_path_val and cs_path_val != '' -%}{%- assign cs_path_arr = cs_path_val | split: ',' -%}{%- for cs_item in cs_path_arr -%}{%- assign cs_item_s = cs_item | strip -%}{%- if cs_item_s != '' -%}{%- unless cs_item_s contains '/' -%}{%- assign cs_item_s = cs_item_s | prepend: '/' -%}{%- endunless -%}{%- assign cs_item_norm = cs_item_s | remove_last: '/' -%}{%- if cs_item_norm == '' -%}{%- assign cs_item_norm = '/' -%}{%- endif -%}{%- assign cs_path_norm = request.path | remove_last: '/' -%}{%- if cs_path_norm == '' -%}{%- assign cs_path_norm = '/' -%}{%- endif -%}{%- assign cs_item_prefix = cs_item_norm | append: '/' -%}{%- if cs_path_norm == cs_item_norm or request.path contains cs_item_prefix -%}{%- assign cs_noindex = true -%}{%- assign cs_matched_rule = 'noindex_specific_url' -%}{%- break -%}{%- endif -%}{%- endif -%}{%- endfor -%}{%- endif -%}{%- endif -%}{%- endif -%}
{%- if cs_noindex -%}
{%- capture cs_robots_val -%}{%- if cs_nofollow -%}noindex,nofollow{%- else -%}noindex,follow{%- endif -%}{%- endcapture -%}
<meta name="robots" content="{{ cs_robots_val }}" data-cleanserp="v2.0.4">
<script>(function(){var c='{{ cs_robots_val }}';var a=document.querySelectorAll('meta[name="robots"]');if(a.length>1){a[0].content=c;a[0].setAttribute('data-cleanserp','v2.0.4');for(var i=1;i<a.length;i++){a[i].remove();}}else if(a.length===1){a[0].setAttribute('data-cleanserp','v2.0.4');}{%- if cs_canonical != '' -%}var u='{{ cs_canonical }}';var l=document.querySelectorAll('link[rel="canonical"]');if(l.length>0){l[0].href=u;l[0].setAttribute('data-cleanserp','v2.0.4');for(var i=1;i<l.length;i++){l[i].remove();}}else{var nl=document.createElement('link');nl.rel='canonical';nl.href=u;nl.setAttribute('data-cleanserp','v2.0.4');document.head.appendChild(nl);}{%- endif -%}if(navigator.sendBeacon){try{var d={shop:'{{ shop.permanent_domain }}',url:location.href,path:location.pathname,template:'{{ template | escape }}',ruleKey:'{{ cs_matched_rule }}',ruleCategory:'liquid',robotsValue:c,canonicalUrl:{%- if cs_canonical != '' -%}u{%- else -%}null{%- endif -%}};navigator.sendBeacon('https://cleanserp-production.up.railway.app/api/track-seo',new Blob([JSON.stringify(d)]));}catch(e){}}})();</script>
{%- endif -%}
{%- unless cs_noindex -%}
{%- if cs_rules.noindex_product_urls_variant_parameter == true or cs_rules.noindex_urls_with_product_recommendations_parameters == true or cs_rules.noindex_custom_url_parameters == true or cs_rules.noindex_collection_urls_with_sort_parameters == true -%}{%- assign cs_needs_param_js = true -%}{%- endif -%}
{%- if cs_needs_param_js -%}
<script>(function(){var s=window.location.search;if(!s)return;var m=false;{%- if cs_rules.noindex_product_urls_variant_parameter == true -%}if(s.indexOf('variant=')>-1)m=true;{%- endif -%}{%- if cs_rules.noindex_urls_with_product_recommendations_parameters == true -%}if(!m&&s.indexOf('pr_prod_strat=')>-1)m=true;{%- endif -%}{%- if cs_rules.noindex_collection_urls_with_sort_parameters == true -%}if(!m&&(s.indexOf('filter.')>-1||s.indexOf('filter=')>-1))m=true;{%- endif -%}{%- if cs_rules.noindex_custom_url_parameters == true -%}{%- assign cs_param_val = cs_rules.noindex_custom_url_parameters_value -%}{%- if cs_param_val and cs_param_val != '' -%}{%- assign cs_param_arr = cs_param_val | split: ',' -%}{%- for cs_item in cs_param_arr -%}{%- assign cs_item_s = cs_item | strip | remove: '?' | remove: '&' -%}{%- if cs_item_s != '' -%}if(!m){var p='{{ cs_item_s | escape }}';if(p.indexOf('=')>-1){if(s.indexOf(p)>-1)m=true;}else{if(s.indexOf(p+'=')>-1||s.indexOf(p+'&')>-1||s.indexOf('?'+p)===0||s.indexOf('&'+p)>-1||s==='?'+p||s.endsWith('&'+p)||s.endsWith('?'+p))m=true;}}{%- endif -%}{%- endfor -%}{%- endif -%}{%- endif -%}if(m){var r=document.querySelector('meta[name="robots"]');if(r){r.content='noindex';r.setAttribute('data-cleanserp','v2.0.4');}else{var t=document.createElement('meta');t.name='robots';t.content='noindex';t.setAttribute('data-cleanserp','v2.0.4');document.head.prepend(t);}if(navigator.sendBeacon){try{var d={shop:'{{ shop.permanent_domain }}',url:location.href,path:location.pathname,template:'{{ template | escape }}',ruleKey:'param-runtime',ruleCategory:'param-runtime',robotsValue:'noindex',canonicalUrl:null};navigator.sendBeacon('https://cleanserp-production.up.railway.app/api/track-seo',new Blob([JSON.stringify(d)]));}catch(e){}}}})();</script>
{%- endif -%}
{%- endunless -%}
{%- if app.metafields.cs_x9k2m.debug_mode.value == true -%}
<script>console.log('[CleanSerp]',{rules:{{ cs_rules | json }},noindex:{{ cs_noindex }},nofollow:{{ cs_nofollow }},canonical:{{ cs_canonical | json }},needs_param_js:{{ cs_needs_param_js }},version:'v2.0.4'});</script>
{%- endif -%}
{%- endif -%}
{% schema %}
{
  "name": "CleanSerp SEO",
  "target": "head",
  "settings": []
}
{% endschema %}
