shopify-google-analytics-helper
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -1,2 +0,2 @@ | ||
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t},t.apply(this,arguments)}var r=function(t){var r=t;return String(t).match(/^\d+$/)?t:(t.match(/^gid:\/\//)||(r=atob(t)),(r=r.match(/\/([^/]+)$/))?r[1]:void 0)};function e(e){var n=e.storeUrl,o=void 0===n?"shops.myshopify.com":n,i=e.storefrontToken,c=void 0===i?"":i,a=e.currencyCode,u=void 0===a?"USD":a,d=e.debug,s=void 0!==d&&d,m=function(r,e){return s&&console.log("'"+r+"'",e),window.dataLayer||(window.dataLayer=[]),e.ecommerce&&window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t({event:r},e))},p=function(t){try{return t?Promise.resolve(function(t){try{return Promise.resolve(fetch("https://"+o+"/api/2021-10/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Shopify-Storefront-Access-Token":c},body:JSON.stringify(t)})).then(function(r){return Promise.resolve(r.json()).then(function(r){var e=r.data,n=r.errors;if(n)throw new Error(n,t);return e})})}catch(t){return Promise.reject(t)}}({variables:{id:btoa("gid://shopify/ProductVariant/"+t)},query:"query($id: ID!) {\n\tnode(id: $id) {\n\t\t...variant\n\t}\n}\nfragment variant on ProductVariant {\n\tid\n\tsku\n\ttitle\n\tprice\n\tcompareAtPrice\n\timage { originalSrc }\n\tproduct {\n\t\tid\n\t\ttitle\n\t\thandle\n\t\tproductType\n\t\tvendor\n\t}\n}"})).then(function(t){return t.node}):Promise.resolve(!1)}catch(t){return Promise.reject(t)}},l=function(t){try{var e=function(e){return e?function(t){var e=t.product,n="https://"+o+"/products/"+e.handle,i=t.image,c=r(t.id);return{productId:r(e.id),productTitle:e.title,productVariantTitle:e.title+" - "+t.title,productType:e.productType||e.type,productVendor:e.vendor,productUrl:n,sku:t.sku,price:t.price,compareAtPrice:t.compareAtPrice,variantId:c,variantTitle:t.title,variantImage:i.originalSrc||t.image,variantUrl:n+"?variant="+c}}(e):console.error("Variant not found",t)};return Promise.resolve("object"==typeof t?e("object"==typeof t?t:p(t)):Promise.resolve("object"==typeof t?t:p(t)).then(e))}catch(t){return Promise.reject(t)}},v=function(t){return{currency:u,item_id:t.sku||t.variantId,item_name:t.productVariantTitle,item_brand:t.productVendor,item_category:t.productType,item_variant:t.variantTitle,price:t.price}},f=function(r){return function(e){return e?m(r,{ecommerce:t({},e)}):null}},h=function(r,e,n){return Promise.resolve(l(r)).then(function(r){return r?m(n,{ecommerce:{items:[t({},v(r),{quantity:e})]}}):null})};return{viewItem:function(r,e){var n=void 0===e?{}:e,o=n.el,i=n.list,c=n.position;return Promise.resolve(l(r)).then(function(r){var e=c||function(t){var r=t,e=0;if(r)for(r=t.previousElementSibling;r;)e+=1;return e}(o);if(!r)return null;var n=t({},v(r),{index:e});return i&&(n.item_list_name=i),m("view_item",{ecommerce:{items:[n]}})})},addToCart:function(t,r){return h(t,r,"add_to_cart")},removeFromCart:function(t,r){return h(t,r,"remove_from_cart")},viewCart:function(r){try{return Promise.resolve(r?m("view_cart",{ecommerce:t({},r)}):null)}catch(t){return Promise.reject(t)}},beginCheckout:function(t){return f("begin_checkout")(t)},purchase:function(t){return f("purchase")(t)}}}export{e as default}; | ||
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},t.apply(this,arguments)}var e=function(t){var e=t;return String(t).match(/^\d+$/)?t:(t.match(/^gid:\/\//)||(e=window.atob(t)),(e=e.match(/\/([^\/]+)$/))?e[1]:void 0)};function r(r){var n=r.storeUrl,i=void 0===n?"shops.myshopify.com":n,o=r.storefrontToken,c=void 0===o?"":o,a=r.currencyCode,u=void 0===a?"USD":a,s=r.debug,d=void 0!==s&&s,m=function(e,r){return d&&console.log("'"+e+"'",r),window.dataLayer||(window.dataLayer=[]),r.ecommerce&&window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t({event:e},r))},l=function(t){try{return t?Promise.resolve(function(t){try{return Promise.resolve(fetch("https://"+i+"/api/2022-07/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Shopify-Storefront-Access-Token":c},body:JSON.stringify(t)})).then(function(e){return Promise.resolve(e.json()).then(function(e){var r=e.data,n=e.errors;if(n){var i=new Error(JSON.stringify(n));throw i.metadata=t,i}return r})})}catch(t){return Promise.reject(t)}}({variables:{id:window.btoa("gid://shopify/ProductVariant/"+t)},query:"query($id: ID!) {\n\tnode(id: $id) {\n\t\t...variant\n\t}\n}\nfragment variant on ProductVariant {\n\tid\n\tsku\n\ttitle\n\tprice\n\tcompareAtPrice\n\timage { originalSrc }\n\tproduct {\n\t\tid\n\t\ttitle\n\t\thandle\n\t\tproductType\n\t\tvendor\n\t}\n}"})).then(function(t){return t.node}):Promise.resolve(void 0)}catch(t){return Promise.reject(t)}},p=function(t){try{var r=function(r){return r?function(t){var r=t.product,n="https://"+i+"/products/"+r.handle,o=t.image,c=e(t.id);return{productId:e(r.id),productTitle:r.title,productVariantTitle:r.title+" - "+t.title,productType:r.productType,productVendor:r.vendor,productUrl:n,sku:t.sku,price:t.price,compareAtPrice:t.compareAtPrice,variantId:c,variantTitle:t.title,variantImage:o.originalSrc||t.image,variantUrl:n+"?variant="+c}}(r):console.error("Variant not found",t)};return Promise.resolve("object"==typeof t?r("object"==typeof t?t:l(t)):Promise.resolve("object"==typeof t?t:l(t)).then(r))}catch(t){return Promise.reject(t)}},v=function(t){return{currency:u,item_id:t.sku||t.variantId,item_name:t.productVariantTitle,item_brand:t.productVendor,item_category:t.productType,item_variant:t.variantTitle,price:t.price}},f=function(e){return function(r,n){var i=void 0===n?{}:n,o=i.el,c=i.list,a=i.position;return Promise.resolve(p(r)).then(function(r){var n=a||function(t){var e=t,r=0;if(e)for(e=t.previousElementSibling;e;)r+=1;return r}(o);if(!r)return null;var i=t({},v(r),{index:n});return c&&(i.item_list_name=c),m(e,{ecommerce:{items:[i]}})})}},h=function(e){return function(r){if(r)return m(e,{ecommerce:t({},r)})}},y=function(e,r,n){return Promise.resolve(p(e)).then(function(e){return e?m(n,{ecommerce:{items:[t({},v(e),{quantity:r})]}}):void 0})};return{viewItem:function(t,e){var r=void 0===e?{}:e,n=r.el,i=r.list,o=r.position;return f("view_item")(t,{el:n,list:i,position:o})},selectItem:function(t,e){var r=void 0===e?{}:e,n=r.el,i=r.list,o=r.position;return f("select_item")(t,{el:n,list:i,position:o})},viewItemList:function(t){if(t)return m("view_item_list",{ecommerce:{items:[].concat(t)}})},addToCart:function(t,e){return y(t,e,"add_to_cart")},removeFromCart:function(t,e){return y(t,e,"remove_from_cart")},viewCart:function(e){try{return Promise.resolve(e?m("view_cart",{ecommerce:t({},e)}):void 0)}catch(t){return Promise.reject(t)}},beginCheckout:function(t){return h("begin_checkout")(t)},purchase:function(t){return h("purchase")(t)}}}export{r as default}; | ||
//# sourceMappingURL=bundle.es.js.map |
@@ -1,2 +0,2 @@ | ||
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t},t.apply(this,arguments)}var r=function(t){var r=t;return String(t).match(/^\d+$/)?t:(t.match(/^gid:\/\//)||(r=atob(t)),(r=r.match(/\/([^/]+)$/))?r[1]:void 0)};module.exports=function(e){var n=e.storeUrl,o=void 0===n?"shops.myshopify.com":n,i=e.storefrontToken,c=void 0===i?"":i,a=e.currencyCode,u=void 0===a?"USD":a,d=e.debug,s=void 0!==d&&d,m=function(r,e){return s&&console.log("'"+r+"'",e),window.dataLayer||(window.dataLayer=[]),e.ecommerce&&window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t({event:r},e))},p=function(t){try{return t?Promise.resolve(function(t){try{return Promise.resolve(fetch("https://"+o+"/api/2021-10/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Shopify-Storefront-Access-Token":c},body:JSON.stringify(t)})).then(function(r){return Promise.resolve(r.json()).then(function(r){var e=r.data,n=r.errors;if(n)throw new Error(n,t);return e})})}catch(t){return Promise.reject(t)}}({variables:{id:btoa("gid://shopify/ProductVariant/"+t)},query:"query($id: ID!) {\n\tnode(id: $id) {\n\t\t...variant\n\t}\n}\nfragment variant on ProductVariant {\n\tid\n\tsku\n\ttitle\n\tprice\n\tcompareAtPrice\n\timage { originalSrc }\n\tproduct {\n\t\tid\n\t\ttitle\n\t\thandle\n\t\tproductType\n\t\tvendor\n\t}\n}"})).then(function(t){return t.node}):Promise.resolve(!1)}catch(t){return Promise.reject(t)}},l=function(t){try{var e=function(e){return e?function(t){var e=t.product,n="https://"+o+"/products/"+e.handle,i=t.image,c=r(t.id);return{productId:r(e.id),productTitle:e.title,productVariantTitle:e.title+" - "+t.title,productType:e.productType||e.type,productVendor:e.vendor,productUrl:n,sku:t.sku,price:t.price,compareAtPrice:t.compareAtPrice,variantId:c,variantTitle:t.title,variantImage:i.originalSrc||t.image,variantUrl:n+"?variant="+c}}(e):console.error("Variant not found",t)};return Promise.resolve("object"==typeof t?e("object"==typeof t?t:p(t)):Promise.resolve("object"==typeof t?t:p(t)).then(e))}catch(t){return Promise.reject(t)}},v=function(t){return{currency:u,item_id:t.sku||t.variantId,item_name:t.productVariantTitle,item_brand:t.productVendor,item_category:t.productType,item_variant:t.variantTitle,price:t.price}},f=function(r){return function(e){return e?m(r,{ecommerce:t({},e)}):null}},h=function(r,e,n){return Promise.resolve(l(r)).then(function(r){return r?m(n,{ecommerce:{items:[t({},v(r),{quantity:e})]}}):null})};return{viewItem:function(r,e){var n=void 0===e?{}:e,o=n.el,i=n.list,c=n.position;return Promise.resolve(l(r)).then(function(r){var e=c||function(t){var r=t,e=0;if(r)for(r=t.previousElementSibling;r;)e+=1;return e}(o);if(!r)return null;var n=t({},v(r),{index:e});return i&&(n.item_list_name=i),m("view_item",{ecommerce:{items:[n]}})})},addToCart:function(t,r){return h(t,r,"add_to_cart")},removeFromCart:function(t,r){return h(t,r,"remove_from_cart")},viewCart:function(r){try{return Promise.resolve(r?m("view_cart",{ecommerce:t({},r)}):null)}catch(t){return Promise.reject(t)}},beginCheckout:function(t){return f("begin_checkout")(t)},purchase:function(t){return f("purchase")(t)}}}; | ||
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},t.apply(this,arguments)}var e=function(t){var e=t;return String(t).match(/^\d+$/)?t:(t.match(/^gid:\/\//)||(e=window.atob(t)),(e=e.match(/\/([^\/]+)$/))?e[1]:void 0)};module.exports=function(r){var n=r.storeUrl,i=void 0===n?"shops.myshopify.com":n,o=r.storefrontToken,c=void 0===o?"":o,a=r.currencyCode,u=void 0===a?"USD":a,s=r.debug,d=void 0!==s&&s,m=function(e,r){return d&&console.log("'"+e+"'",r),window.dataLayer||(window.dataLayer=[]),r.ecommerce&&window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t({event:e},r))},l=function(t){try{return t?Promise.resolve(function(t){try{return Promise.resolve(fetch("https://"+i+"/api/2022-07/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Shopify-Storefront-Access-Token":c},body:JSON.stringify(t)})).then(function(e){return Promise.resolve(e.json()).then(function(e){var r=e.data,n=e.errors;if(n){var i=new Error(JSON.stringify(n));throw i.metadata=t,i}return r})})}catch(t){return Promise.reject(t)}}({variables:{id:window.btoa("gid://shopify/ProductVariant/"+t)},query:"query($id: ID!) {\n\tnode(id: $id) {\n\t\t...variant\n\t}\n}\nfragment variant on ProductVariant {\n\tid\n\tsku\n\ttitle\n\tprice\n\tcompareAtPrice\n\timage { originalSrc }\n\tproduct {\n\t\tid\n\t\ttitle\n\t\thandle\n\t\tproductType\n\t\tvendor\n\t}\n}"})).then(function(t){return t.node}):Promise.resolve(void 0)}catch(t){return Promise.reject(t)}},p=function(t){try{var r=function(r){return r?function(t){var r=t.product,n="https://"+i+"/products/"+r.handle,o=t.image,c=e(t.id);return{productId:e(r.id),productTitle:r.title,productVariantTitle:r.title+" - "+t.title,productType:r.productType,productVendor:r.vendor,productUrl:n,sku:t.sku,price:t.price,compareAtPrice:t.compareAtPrice,variantId:c,variantTitle:t.title,variantImage:o.originalSrc||t.image,variantUrl:n+"?variant="+c}}(r):console.error("Variant not found",t)};return Promise.resolve("object"==typeof t?r("object"==typeof t?t:l(t)):Promise.resolve("object"==typeof t?t:l(t)).then(r))}catch(t){return Promise.reject(t)}},v=function(t){return{currency:u,item_id:t.sku||t.variantId,item_name:t.productVariantTitle,item_brand:t.productVendor,item_category:t.productType,item_variant:t.variantTitle,price:t.price}},f=function(e){return function(r,n){var i=void 0===n?{}:n,o=i.el,c=i.list,a=i.position;return Promise.resolve(p(r)).then(function(r){var n=a||function(t){var e=t,r=0;if(e)for(e=t.previousElementSibling;e;)r+=1;return r}(o);if(!r)return null;var i=t({},v(r),{index:n});return c&&(i.item_list_name=c),m(e,{ecommerce:{items:[i]}})})}},h=function(e){return function(r){if(r)return m(e,{ecommerce:t({},r)})}},y=function(e,r,n){return Promise.resolve(p(e)).then(function(e){return e?m(n,{ecommerce:{items:[t({},v(e),{quantity:r})]}}):void 0})};return{viewItem:function(t,e){var r=void 0===e?{}:e,n=r.el,i=r.list,o=r.position;return f("view_item")(t,{el:n,list:i,position:o})},selectItem:function(t,e){var r=void 0===e?{}:e,n=r.el,i=r.list,o=r.position;return f("select_item")(t,{el:n,list:i,position:o})},viewItemList:function(t){if(t)return m("view_item_list",{ecommerce:{items:[].concat(t)}})},addToCart:function(t,e){return y(t,e,"add_to_cart")},removeFromCart:function(t,e){return y(t,e,"remove_from_cart")},viewCart:function(e){try{return Promise.resolve(e?m("view_cart",{ecommerce:t({},e)}):void 0)}catch(t){return Promise.reject(t)}},beginCheckout:function(t){return h("begin_checkout")(t)},purchase:function(t){return h("purchase")(t)}}}; | ||
//# sourceMappingURL=bundle.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).shopifyGoogleAnalyticsHelper=e()}(this,function(){function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},t.apply(this,arguments)}var e=function(t){var e=t;return String(t).match(/^\d+$/)?t:(t.match(/^gid:\/\//)||(e=atob(t)),(e=e.match(/\/([^/]+)$/))?e[1]:void 0)};return function(r){var n=r.storeUrl,o=void 0===n?"shops.myshopify.com":n,i=r.storefrontToken,c=void 0===i?"":i,a=r.currencyCode,u=void 0===a?"USD":a,d=r.debug,s=void 0!==d&&d,l=function(e,r){return s&&console.log("'"+e+"'",r),window.dataLayer||(window.dataLayer=[]),r.ecommerce&&window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t({event:e},r))},p=function(t){try{return t?Promise.resolve(function(t){try{return Promise.resolve(fetch("https://"+o+"/api/2021-10/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Shopify-Storefront-Access-Token":c},body:JSON.stringify(t)})).then(function(e){return Promise.resolve(e.json()).then(function(e){var r=e.data,n=e.errors;if(n)throw new Error(n,t);return r})})}catch(t){return Promise.reject(t)}}({variables:{id:btoa("gid://shopify/ProductVariant/"+t)},query:"query($id: ID!) {\n\tnode(id: $id) {\n\t\t...variant\n\t}\n}\nfragment variant on ProductVariant {\n\tid\n\tsku\n\ttitle\n\tprice\n\tcompareAtPrice\n\timage { originalSrc }\n\tproduct {\n\t\tid\n\t\ttitle\n\t\thandle\n\t\tproductType\n\t\tvendor\n\t}\n}"})).then(function(t){return t.node}):Promise.resolve(!1)}catch(t){return Promise.reject(t)}},f=function(t){try{var r=function(r){return r?function(t){var r=t.product,n="https://"+o+"/products/"+r.handle,i=t.image,c=e(t.id);return{productId:e(r.id),productTitle:r.title,productVariantTitle:r.title+" - "+t.title,productType:r.productType||r.type,productVendor:r.vendor,productUrl:n,sku:t.sku,price:t.price,compareAtPrice:t.compareAtPrice,variantId:c,variantTitle:t.title,variantImage:i.originalSrc||t.image,variantUrl:n+"?variant="+c}}(r):console.error("Variant not found",t)};return Promise.resolve("object"==typeof t?r("object"==typeof t?t:p(t)):Promise.resolve("object"==typeof t?t:p(t)).then(r))}catch(t){return Promise.reject(t)}},m=function(t){return{currency:u,item_id:t.sku||t.variantId,item_name:t.productVariantTitle,item_brand:t.productVendor,item_category:t.productType,item_variant:t.variantTitle,price:t.price}},v=function(e){return function(r){return r?l(e,{ecommerce:t({},r)}):null}},h=function(e,r,n){return Promise.resolve(f(e)).then(function(e){return e?l(n,{ecommerce:{items:[t({},m(e),{quantity:r})]}}):null})};return{viewItem:function(e,r){var n=void 0===r?{}:r,o=n.el,i=n.list,c=n.position;return Promise.resolve(f(e)).then(function(e){var r=c||function(t){var e=t,r=0;if(e)for(e=t.previousElementSibling;e;)r+=1;return r}(o);if(!e)return null;var n=t({},m(e),{index:r});return i&&(n.item_list_name=i),l("view_item",{ecommerce:{items:[n]}})})},addToCart:function(t,e){return h(t,e,"add_to_cart")},removeFromCart:function(t,e){return h(t,e,"remove_from_cart")},viewCart:function(e){try{return Promise.resolve(e?l("view_cart",{ecommerce:t({},e)}):null)}catch(t){return Promise.reject(t)}},beginCheckout:function(t){return v("begin_checkout")(t)},purchase:function(t){return v("purchase")(t)}}}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).shopifyGoogleAnalyticsHelper=e()}(this,function(){function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},t.apply(this,arguments)}var e=function(t){var e=t;return String(t).match(/^\d+$/)?t:(t.match(/^gid:\/\//)||(e=window.atob(t)),(e=e.match(/\/([^\/]+)$/))?e[1]:void 0)};return function(r){var n=r.storeUrl,i=void 0===n?"shops.myshopify.com":n,o=r.storefrontToken,c=void 0===o?"":o,a=r.currencyCode,u=void 0===a?"USD":a,d=r.debug,s=void 0!==d&&d,l=function(e,r){return s&&console.log("'"+e+"'",r),window.dataLayer||(window.dataLayer=[]),r.ecommerce&&window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t({event:e},r))},m=function(t){try{return t?Promise.resolve(function(t){try{return Promise.resolve(fetch("https://"+i+"/api/2022-07/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Shopify-Storefront-Access-Token":c},body:JSON.stringify(t)})).then(function(e){return Promise.resolve(e.json()).then(function(e){var r=e.data,n=e.errors;if(n){var i=new Error(JSON.stringify(n));throw i.metadata=t,i}return r})})}catch(t){return Promise.reject(t)}}({variables:{id:window.btoa("gid://shopify/ProductVariant/"+t)},query:"query($id: ID!) {\n\tnode(id: $id) {\n\t\t...variant\n\t}\n}\nfragment variant on ProductVariant {\n\tid\n\tsku\n\ttitle\n\tprice\n\tcompareAtPrice\n\timage { originalSrc }\n\tproduct {\n\t\tid\n\t\ttitle\n\t\thandle\n\t\tproductType\n\t\tvendor\n\t}\n}"})).then(function(t){return t.node}):Promise.resolve(void 0)}catch(t){return Promise.reject(t)}},f=function(t){try{var r=function(r){return r?function(t){var r=t.product,n="https://"+i+"/products/"+r.handle,o=t.image,c=e(t.id);return{productId:e(r.id),productTitle:r.title,productVariantTitle:r.title+" - "+t.title,productType:r.productType,productVendor:r.vendor,productUrl:n,sku:t.sku,price:t.price,compareAtPrice:t.compareAtPrice,variantId:c,variantTitle:t.title,variantImage:o.originalSrc||t.image,variantUrl:n+"?variant="+c}}(r):console.error("Variant not found",t)};return Promise.resolve("object"==typeof t?r("object"==typeof t?t:m(t)):Promise.resolve("object"==typeof t?t:m(t)).then(r))}catch(t){return Promise.reject(t)}},p=function(t){return{currency:u,item_id:t.sku||t.variantId,item_name:t.productVariantTitle,item_brand:t.productVendor,item_category:t.productType,item_variant:t.variantTitle,price:t.price}},v=function(e){return function(r,n){var i=void 0===n?{}:n,o=i.el,c=i.list,a=i.position;return Promise.resolve(f(r)).then(function(r){var n=a||function(t){var e=t,r=0;if(e)for(e=t.previousElementSibling;e;)r+=1;return r}(o);if(!r)return null;var i=t({},p(r),{index:n});return c&&(i.item_list_name=c),l(e,{ecommerce:{items:[i]}})})}},h=function(e){return function(r){if(r)return l(e,{ecommerce:t({},r)})}},y=function(e,r,n){return Promise.resolve(f(e)).then(function(e){return e?l(n,{ecommerce:{items:[t({},p(e),{quantity:r})]}}):void 0})};return{viewItem:function(t,e){var r=void 0===e?{}:e,n=r.el,i=r.list,o=r.position;return v("view_item")(t,{el:n,list:i,position:o})},selectItem:function(t,e){var r=void 0===e?{}:e,n=r.el,i=r.list,o=r.position;return v("select_item")(t,{el:n,list:i,position:o})},viewItemList:function(t){if(t)return l("view_item_list",{ecommerce:{items:[].concat(t)}})},addToCart:function(t,e){return y(t,e,"add_to_cart")},removeFromCart:function(t,e){return y(t,e,"remove_from_cart")},viewCart:function(e){try{return Promise.resolve(e?l("view_cart",{ecommerce:t({},e)}):void 0)}catch(t){return Promise.reject(t)}},beginCheckout:function(t){return h("begin_checkout")(t)},purchase:function(t){return h("purchase")(t)}}}}); | ||
//# sourceMappingURL=bundle.umd.js.map |
{ | ||
"name": "shopify-google-analytics-helper", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
121
src/index.js
@@ -11,3 +11,3 @@ import { fetchVariantQuery } from "./queries" | ||
const queryStorefrontApi = async (payload) => { | ||
const response = await fetch(`https://${storeUrl}/api/2021-10/graphql`, { | ||
const response = await fetch(`https://${storeUrl}/api/2022-07/graphql`, { | ||
method: "POST", | ||
@@ -25,3 +25,6 @@ headers: { | ||
if (errors) { | ||
throw new Error(errors, payload) | ||
const error = new Error(JSON.stringify(errors)) | ||
error.metadata = payload | ||
throw error | ||
} | ||
@@ -32,5 +35,5 @@ | ||
const pushEvent = (name, payload) => { | ||
const pushDataLayerEvent = (gtmEvent, payload) => { | ||
if (debug) { | ||
console.log(`'${name}'`, payload) | ||
console.log(`'${gtmEvent}'`, payload) | ||
} | ||
@@ -51,3 +54,3 @@ | ||
return window.dataLayer.push({ | ||
event: name, | ||
event: gtmEvent, | ||
...payload, | ||
@@ -59,3 +62,3 @@ }) | ||
if (!variantId) { | ||
return false | ||
return undefined | ||
} | ||
@@ -65,3 +68,3 @@ | ||
variables: { | ||
id: btoa(`gid://shopify/ProductVariant/${variantId}`), | ||
id: window.btoa(`gid://shopify/ProductVariant/${variantId}`), | ||
}, | ||
@@ -75,3 +78,3 @@ query: fetchVariantQuery, | ||
/** | ||
* Make flat object from a variant with nested product data | ||
* Make flat object from a variant with nested product data. | ||
*/ | ||
@@ -85,10 +88,8 @@ const makeFlatVariant = (variant) => { | ||
return { | ||
// Product level info | ||
productId: getShopifyId(product.id), | ||
productTitle: product.title, | ||
productVariantTitle: `${product.title} - ${variant.title}`, | ||
productType: product.productType || product.type, | ||
productType: product.productType, | ||
productVendor: product.vendor, | ||
productUrl, | ||
// Variant level data | ||
sku: variant.sku, | ||
@@ -127,29 +128,43 @@ price: variant.price, | ||
const viewItem = async (variantPayload, { el, list, position } = {}) => { | ||
const flatVariant = await getFlatVariant(variantPayload) | ||
const index = position || getElementPosition(el) | ||
const viewOrSelectItem = | ||
(gtmEvent) => | ||
async (variantPayload, { el, list, position } = {}) => { | ||
const flatVariant = await getFlatVariant(variantPayload) | ||
const index = position || getElementPosition(el) | ||
if (!flatVariant) { | ||
return null | ||
} | ||
if (!flatVariant) { | ||
return null | ||
} | ||
const item = { | ||
...makeGaProductFieldObject(flatVariant), | ||
index, | ||
} | ||
const item = { | ||
...makeGaProductFieldObject(flatVariant), | ||
index, | ||
} | ||
if (list) { | ||
item.item_list_name = list | ||
if (list) { | ||
item.item_list_name = list | ||
} | ||
return pushDataLayerEvent(gtmEvent, { | ||
ecommerce: { | ||
items: [item], | ||
}, | ||
}) | ||
} | ||
return pushEvent("view_item", { | ||
ecommerce: { | ||
items: [item], | ||
}, | ||
}) | ||
const viewItem = (variantPayload, { el, list, position } = {}) => { | ||
const pushEvent = viewOrSelectItem("view_item") | ||
return pushEvent(variantPayload, { el, list, position }) | ||
} | ||
const selectItem = (variantPayload, { el, list, position } = {}) => { | ||
const pushEvent = viewOrSelectItem("select_item") | ||
return pushEvent(variantPayload, { el, list, position }) | ||
} | ||
const viewCart = async (checkoutOrCartPayload) => { | ||
if (checkoutOrCartPayload) { | ||
return pushEvent("view_cart", { | ||
return pushDataLayerEvent("view_cart", { | ||
ecommerce: { | ||
@@ -161,3 +176,3 @@ ...checkoutOrCartPayload, | ||
return null | ||
return undefined | ||
} | ||
@@ -167,3 +182,3 @@ | ||
if (checkoutOrCartPayload) { | ||
return pushEvent(gtmEvent, { | ||
return pushDataLayerEvent(gtmEvent, { | ||
ecommerce: { | ||
@@ -175,17 +190,41 @@ ...checkoutOrCartPayload, | ||
return null | ||
return undefined | ||
} | ||
const beginCheckout = (checkoutOrCartPayload) => { | ||
const fn = checkoutOrCart("begin_checkout") | ||
const pushEvent = checkoutOrCart("begin_checkout") | ||
return fn(checkoutOrCartPayload) | ||
return pushEvent(checkoutOrCartPayload) | ||
} | ||
const purchase = (checkoutOrCartPayload) => { | ||
const fn = checkoutOrCart("purchase") | ||
const pushEvent = checkoutOrCart("purchase") | ||
return fn(checkoutOrCartPayload) | ||
return pushEvent(checkoutOrCartPayload) | ||
} | ||
/** | ||
* | ||
* @param {Array.<Object>} itemsPayload | ||
* @returns | ||
*/ | ||
const viewItemList = (itemsPayload) => { | ||
if (itemsPayload) { | ||
return pushDataLayerEvent("view_item_list", { | ||
ecommerce: { | ||
items: [...itemsPayload], | ||
}, | ||
}) | ||
} | ||
return undefined | ||
} | ||
/** | ||
* Sends event of changes in the quantity of items in the cart. | ||
* @param {(Object.<string, (string|number)>|number|string)} variantPayload | ||
* @param {(number|string)} quantity | ||
* @param {string} gtmEvent | ||
* @returns | ||
*/ | ||
const updateQuantity = async (variantPayload, quantity, gtmEvent) => { | ||
@@ -195,6 +234,6 @@ const flatVariant = await getFlatVariant(variantPayload) | ||
if (!flatVariant) { | ||
return null | ||
return undefined | ||
} | ||
return pushEvent(gtmEvent, { | ||
return pushDataLayerEvent(gtmEvent, { | ||
ecommerce: { | ||
@@ -213,3 +252,3 @@ items: [ | ||
* Used whenever there is a positive change in the quantity of a product in | ||
* the cart. | ||
* the cart or add new product to cart. | ||
*/ | ||
@@ -221,3 +260,3 @@ const addToCart = (variantPayload, quantity) => | ||
* Used whenever there is a negative change in the quantity of a product in | ||
* the cart. | ||
* the cart or remove product from cart. | ||
*/ | ||
@@ -229,2 +268,4 @@ const removeFromCart = (variantPayload, quantity) => | ||
viewItem, | ||
selectItem, | ||
viewItemList, | ||
addToCart, | ||
@@ -231,0 +272,0 @@ removeFromCart, |
@@ -1,2 +0,1 @@ | ||
// Product Variant fragment | ||
export const productVariantFragment = `fragment variant on ProductVariant { | ||
@@ -18,3 +17,2 @@ id | ||
// Graphql query to fetch a variant by id | ||
export const fetchVariantQuery = `query($id: ID!) { | ||
@@ -26,51 +24,1 @@ node(id: $id) { | ||
${productVariantFragment}` | ||
// Graphql query to fetch a cart by id | ||
export const fetchCartQuery = `query($id: ID!) { | ||
node: cart(id: $id) { | ||
... on Cart { | ||
id | ||
webUrl: checkoutUrl | ||
estimatedCost { | ||
subtotalAmount { amount } | ||
totalAmount { amount } | ||
} | ||
lineItems: lines (first: 250) { | ||
edges { | ||
node { | ||
... on CartLine { | ||
id | ||
quantity | ||
variant: merchandise { ...variant } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
${productVariantFragment}` | ||
// Graphql query to fetch a checkout by id | ||
export const fetchCheckoutQuery = `query($id: ID!) { | ||
node(id: $id) { | ||
... on Checkout { | ||
id | ||
webUrl | ||
subtotalPrice | ||
totalPrice | ||
lineItems (first: 250) { | ||
edges { | ||
node { | ||
... on CheckoutLineItem { | ||
id | ||
quantity | ||
variant { ...variant } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
${productVariantFragment}` |
/** | ||
* Get the id from a Shopify gid:// style id. This strips everything but the | ||
* last part of the string. So gid://shopify/ProductVariant/34641879105581 | ||
* becomes 34641879105581 | ||
* https://regex101.com/r/3FIplL/1 | ||
* Get the identifier from the Shopify style identifier gid:// . | ||
* This removes everything but the last part of the string. | ||
* | ||
* @example | ||
* getShopifyId(34641879105581); | ||
* // returns 34641879105581 | ||
* | ||
* @example | ||
* getShopifyId("Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zNDY0MTg3OTEwNTU4MQ=="); | ||
* // returns "34641879105581" | ||
* | ||
* @example | ||
* getShopifyId("gid://shopify/ProductVariant/34641879105581"); | ||
* // returns "34641879105581" | ||
* | ||
* @param {(string|number)} id | ||
* @returns {(number|string|undefined)} | ||
*/ | ||
@@ -16,7 +29,8 @@ export const getShopifyId = (id) => { | ||
if (!id.match(/^gid:\/\//)) { | ||
// De-base64 | ||
shopifyId = atob(id) | ||
// Decodes a id which has been encoded using Base64 encoding | ||
shopifyId = window.atob(id) | ||
} | ||
shopifyId = shopifyId.match(/\/([^/]+)$/) | ||
// eslint-disable-next-line no-useless-escape | ||
shopifyId = shopifyId.match(/\/([^\/]+)$/) | ||
@@ -27,7 +41,9 @@ return shopifyId ? shopifyId[1] : undefined | ||
/** | ||
* Get the position of an element with respect to it's parent | ||
* Get the position of an element with respect to it's parent. | ||
* @param {(HTMLElement|undefined)} el | ||
* @returns {number} | ||
*/ | ||
export const getElementPosition = (el) => { | ||
let child = el | ||
let i = 0 // The first position will be `0` | ||
let i = 0 | ||
@@ -46,4 +62,5 @@ if (child) { | ||
/** | ||
* Fire callback when in viewport. Not exposing a way to manually disconnect or | ||
* unobserve because it _should_ be garbage collected when el is removed. | ||
* Fire callback once when in viewport. | ||
* @param {HTMLElement} el | ||
* @param {function} callback | ||
*/ | ||
@@ -50,0 +67,0 @@ export const whenFirstInViewport = (el, callback) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
79923
378