Skip to content

Commit

Permalink
Tavano/fix shopify product type (#270)
Browse files Browse the repository at this point in the history
* fix shopify product type param

* fmt
  • Loading branch information
guitavano authored Jan 2, 2024
1 parent 165515f commit 20b4d82
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
13 changes: 11 additions & 2 deletions analytics/loaders/DecoAnalyticsScript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AppContext } from "../mod.ts";
import { Script } from "../../website/types.ts";
import { defaultExclusionPropsAndHashScript, exclusionPropsAndHashScript } from "../scripts/plausible_scripts.ts";
import {
defaultExclusionPropsAndHashScript,
exclusionPropsAndHashScript,
} from "../scripts/plausible_scripts.ts";
import { getFlagsFromCookies } from "../../utils/cookie.ts";

export type Props = {
Expand Down Expand Up @@ -29,7 +32,13 @@ const loader = (
) => (`event-${flagName}="${flagActive}"`)).join(
" ",
)
} ${props.domain ? "data-domain=" + props.domain : ""} data-api="https://plausible.io/api/event">${props.domain ? defaultExclusionPropsAndHashScript : exclusionPropsAndHashScript}</script>`;
} ${
props.domain ? "data-domain=" + props.domain : ""
} data-api="https://plausible.io/api/event">${
props.domain
? defaultExclusionPropsAndHashScript
: exclusionPropsAndHashScript
}</script>`;
return dnsPrefetchLink + preconnectLink + plausibleScript;
};
return ({ src: transformReq });
Expand Down
4 changes: 2 additions & 2 deletions analytics/scripts/plausible_scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export const exclusionPropsAndHashScript =
'!function(){"use strict";var l=window.location,c=window.document,s=c.currentScript,u=s.getAttribute("data-api")||new URL(s.src).origin+"/api/event";function p(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){if(/^localhost$|^127(\\.[0-9]+){0,2}\\.[0-9]+$|^\\[::1?\\]$/.test(l.hostname)||"file:"===l.protocol)return p("localhost",t);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return p(null,t);try{if("true"===window.localStorage.plausible_ignore)return p("localStorage flag",t)}catch(e){}var n=s&&s.getAttribute("data-include"),i=s&&s.getAttribute("data-exclude");if("pageview"===e){n=!n||n.split(",").some(a),i=i&&i.split(",").some(a);if(!n||i)return p("exclusion rule",t)}function a(e){var t=l.pathname;return(t+=l.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var n={},i=(n.n=e,n.u=l.href,n.d=((w,d)=>{const h=w.location.hostname;return h.replace(/^www./,"")})(window,document),n.r=c.referrer||null,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=t.props),s.getAttributeNames().filter(function(e){return"event-"===e.substring(0,6)})),r=n.p||{},o=(i.forEach(function(e){var t=e.replace("event-",""),e=s.getAttribute(e);r[t]=r[t]||e}),n.p=r,n.h=1,new XMLHttpRequest);o.open("POST",u,!0),o.setRequestHeader("Content-Type","text/plain"),o.send(JSON.stringify(n)),o.onreadystatechange=function(){4===o.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var n,i=0;i<t.length;i++)e.apply(this,t[i]);function a(){n=l.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===c.visibilityState?c.addEventListener("visibilitychange",function(){n||"visible"!==c.visibilityState||a()}):a()}();';

// normal script to be used when data-domain is defined.
export const defaultExclusionPropsAndHashScript =
'!function(){"use strict";var o=window.location,l=window.document,c=l.currentScript,s=c.getAttribute("data-api")||new URL(c.src).origin+"/api/event";function u(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){if(/^localhost$|^127(\\.[0-9]+){0,2}\\.[0-9]+$|^\\[::1?\\]$/.test(o.hostname)||"file:"===o.protocol)return u("localhost",t);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return u(null,t);try{if("true"===window.localStorage.plausible_ignore)return u("localStorage flag",t)}catch(e){}var i=c&&c.getAttribute("data-include"),n=c&&c.getAttribute("data-exclude");if("pageview"===e){i=!i||i.split(",").some(a),n=n&&n.split(",").some(a);if(!i||n)return u("exclusion rule",t)}function a(e){var t=o.pathname;return(t+=o.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var i={},r=(i.n=e,i.u=o.href,i.d=c.getAttribute("data-domain"),i.r=l.referrer||null,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=t.props),i.h=1,new XMLHttpRequest);r.open("POST",s,!0),r.setRequestHeader("Content-Type","text/plain"),r.send(JSON.stringify(i)),r.onreadystatechange=function(){4===r.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var i,n=0;n<t.length;n++)e.apply(this,t[n]);function a(){i=o.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===l.visibilityState?l.addEventListener("visibilitychange",function(){i||"visible"!==l.visibilityState||a()}):a()}();'
export const defaultExclusionPropsAndHashScript =
'!function(){"use strict";var o=window.location,l=window.document,c=l.currentScript,s=c.getAttribute("data-api")||new URL(c.src).origin+"/api/event";function u(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){if(/^localhost$|^127(\\.[0-9]+){0,2}\\.[0-9]+$|^\\[::1?\\]$/.test(o.hostname)||"file:"===o.protocol)return u("localhost",t);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return u(null,t);try{if("true"===window.localStorage.plausible_ignore)return u("localStorage flag",t)}catch(e){}var i=c&&c.getAttribute("data-include"),n=c&&c.getAttribute("data-exclude");if("pageview"===e){i=!i||i.split(",").some(a),n=n&&n.split(",").some(a);if(!i||n)return u("exclusion rule",t)}function a(e){var t=o.pathname;return(t+=o.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var i={},r=(i.n=e,i.u=o.href,i.d=c.getAttribute("data-domain"),i.r=l.referrer||null,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=t.props),i.h=1,new XMLHttpRequest);r.open("POST",s,!0),r.setRequestHeader("Content-Type","text/plain"),r.send(JSON.stringify(i)),r.onreadystatechange=function(){4===r.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var i,n=0;n<t.length;n++)e.apply(this,t[n]);function a(){i=o.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===l.visibilityState?l.addEventListener("visibilitychange",function(){i||"visible"!==l.visibilityState||a()}):a()}();';
4 changes: 4 additions & 0 deletions shopify/loaders/ProductListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const loader = async (
| ProductConnection
| undefined = undefined;
let shopifyFilters = undefined;
let records = undefined;

const sort = url.searchParams.get("sort") ?? "";

Expand All @@ -84,6 +85,7 @@ const loader = async (

shopifyProducts = data.search;
shopifyFilters = data.search?.productFilters;
records = data.search?.totalCount;
hasNextPage = Boolean(data?.search?.pageInfo.hasNextPage ?? false);
hasPreviousPage = Boolean(
data?.search?.pageInfo.hasPreviousPage ?? false,
Expand Down Expand Up @@ -111,6 +113,7 @@ const loader = async (

shopifyProducts = data.collection?.products;
shopifyFilters = data.collection?.products?.filters;
console.log(data.collection);
hasNextPage = Boolean(
data?.collection?.products.pageInfo.hasNextPage ?? false,
);
Expand Down Expand Up @@ -169,6 +172,7 @@ const loader = async (
nextPage: hasNextPage ? `?${nextPage}` : undefined,
previousPage: hasPreviousPage ? `?${previousPage}` : undefined,
currentPage: page,
records,
},
sortOptions: isSearch ? searchSortOptions : sortOptions,
};
Expand Down
27 changes: 14 additions & 13 deletions shopify/utils/storefront/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ export const SearchProducts = {
productFilters: $productFilters,
types: PRODUCT,
sortKey: $sortKey,
reverse: $reverse
reverse: $reverse,
){
totalCount
pageInfo {
hasNextPage
hasPreviousPage
Expand Down Expand Up @@ -284,18 +285,18 @@ export const ProductsByCollection = {
reverse: $reverse,
filters: $filters
){
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
filters {
...Filter
}
nodes {
...Product
}
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
filters {
...Filter
}
nodes {
...Product
}
}
}
}`,
Expand Down
2 changes: 2 additions & 0 deletions shopify/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export const getFiltersByUrl = (url: URL) => {
filters.push({ tag: value });
} else if (key.startsWith("filter.p.type")) {
filters.push({ productType: value });
} else if (key.startsWith("filter.p.product_type")) {
filters.push({ productType: value });
} else if (key.startsWith("filter.p.vendor")) {
filters.push({ productVendor: value });
} else if (key.startsWith("filter.v.availability")) {
Expand Down

0 comments on commit 20b4d82

Please sign in to comment.