Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shopify metadata to ecommerce.js #94

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions dist/ecommerce.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
//[ecommerce]
// Uncomment the previous line for testing on webpagetest.org

// README! Instructions for adding a new custom metric for the Web Almanac.
//
// 1. Refer for instructions for addding a custom metric in almanamc.js.
// 2. This file has a special case where a custom metric uses 'fetch' and in that case we need to return a promise that resolves to JSON
// 3. Test your change by following the instructions at https://github.com/HTTPArchive/almanac.httparchive.org/issues/33#issuecomment-502288773.
// 4. Submit a PR to update this file.

function fetchWithTimeout(url) {
var controller = new AbortController();
setTimeout(() => {controller.abort()}, 5000);
return fetch(url, {signal: controller.signal});
}

function getShopifyMetadata() {
return window.Shopify;
}

return Promise.all([
fetchWithTimeout('/.well-known/assetlinks.json').then(function(r) {
if(!r.redirected && r.status === 200) {
Expand All @@ -30,7 +27,18 @@ return Promise.all([
}
})
]).then(([AndroidAppLinks, iOSUniveralLinks]) => {
return JSON.stringify({AndroidAppLinks, iOSUniveralLinks});
return {
AndroidAppLinks,
iOSUniveralLinks
};
}).catch(error => {
return JSON.stringify({message: error.message, error: error});
return {
message: error.message,
error: error
};
}).then(result => {
return {
...result,
Shopify: getShopifyMetadata()
}
});