Skip to content

Instantly share code, notes, and snippets.

@mariusbolik
Created February 14, 2024 09:21
Show Gist options
  • Save mariusbolik/b82ab26478610e0f8e8da518a5d1a9db to your computer and use it in GitHub Desktop.
Save mariusbolik/b82ab26478610e0f8e8da518a5d1a9db to your computer and use it in GitHub Desktop.
mySHOEFITTER x Orbisana Demo Code
<!-- Load the mySHOEFITTER Script -->
<script src="https://js.myshoefitter.com/v1/script.js"></script>
<!-- Initialize mySHOEFITTER -->
<script type="application/javascript">
myshoefitter.init({
shopId: '65cc7e70ba912c607ca8', // Orbisana
productId: '140847915' // Hawaii
});
</script>
<!-- EXTENDED VERSION FOR ALL SHOES / DYNAMIC ID -->
<!-- Load the mySHOEFITTER Script -->
<script src="https://js.myshoefitter.com/v1/script.js"></script>
<!-- Initialize mySHOEFITTER -->
<script type="application/javascript">
// Function to retrieve the item ID from either window.DY.recommendationContext.data or window.dataLayer
function retrieveItemId() {
var id = null;
// First attempt: Try to get the ID from window.DY.recommendationContext.data
try {
if (window.DY && window.DY.recommendationContext && Array.isArray(window.DY.recommendationContext.data) && window.DY.recommendationContext.data.length > 0) {
id = window.DY.recommendationContext.data[0];
}
} catch (e) {
console.log("mySHOEFITTER: Error accessing the ID from DY.recommendationContext:", e);
}
// If the ID was not found in the first attempt, try the second source: window.dataLayer
if (id === null) {
try {
if (Array.isArray(window.dataLayer) && window.dataLayer.length > 0) {
var lastEcommerceObject = window.dataLayer.slice().reverse().find(obj => obj.ecommerce && Array.isArray(obj.ecommerce.items) && obj.ecommerce.items.length > 0);
id = lastEcommerceObject ? lastEcommerceObject.ecommerce.items[0].item_id : null;
}
} catch (e) {
console.log("mySHOEFITTER: Error accessing the item_id from dataLayer:", e);
}
}
return id; // Return the found ID or null if not found
}
// Use the retrieveItemId function to dynamically get the productId
const productId = retrieveItemId();
// Initialize myshoefitter with the retrieved productId, if available
if (productId !== null) {
myshoefitter.init({
shopId: '65cc7e70ba912c607ca8',
productId: productId
});
} else {
console.log("mySHOEFITTER: Product ID not found. myshoefitter initialization skipped.");
const button = document.getElementById('myshoefitter-button');
if (button) {
button.style.display = 'none';
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment