Skip to content

Instantly share code, notes, and snippets.

@guilhermemarconi
Last active November 19, 2020 18:41
Show Gist options
  • Save guilhermemarconi/ee7e9eadc0a0dc681e2bb0fa11382173 to your computer and use it in GitHub Desktop.
Save guilhermemarconi/ee7e9eadc0a0dc681e2bb0fa11382173 to your computer and use it in GitHub Desktop.
Rich Relevance Tracking Codes (VTEX)
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
window.R3_ERROR = new r3_error();
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Error Page -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
$(window).on('farm.addItemToCart', function(_, data) {
configureR3().then(function() {
window.R3_ADDTOCART = window.R3_ADDTOCART || new r3_addtocart();
window.R3_ADDTOCART.addItemIdToCart(data.productId, data.skuId);
rr_flush_onload();
r3();
});
});
} catch (error) {
console.error('GTM -> RR Add to cart -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
window.R3_COMMON.setPageBrand(window.location.pathname.split('/').pop());
window.R3_BRAND = new r3_brand();
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Brand -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
window.R3_CATEGORY = new r3_category();
window.R3_CATEGORY.setId(window.vtxctx.categoryId);
window.R3_CATEGORY.setName({{CAD-DLV-CATEGORY NAME}});
var dataLayerProducts = {{CAD-DLV-SHELF PRODUCT IDS}};
var i = 0;
for (; i < 15; i++) {
if (!dataLayerProducts[i]) break;
window.R3_COMMON.addItemId(dataLayerProducts[i]);
}
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Category -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
window.R3_CART = new r3_cart();
vtexjs.checkout.getOrderForm(['items']).done(function(orderForm) {
orderForm.items.forEach(function(item) {
window.R3_CART.addItemId(item.productId, item.id);
});
});
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Cart page -> error', error);
}
</script>
<script src="//media.richrelevance.com/rrserver/js/1.2/p13n.js"></script>
<script charset="utf-8" type="text/javascript">
RR.jsonCallback = function() {
console.dir(RR.data.JSON.placements);
};
</script>
<script charset="utf-8" type="text/javascript">
function getSession() {
return fetch('/api/sessions?items=profile.id').then(function(response) {
if (!response.ok) throw new Error(response);
return response.json();
}).catch(function(error) {
console.error('GTM -> getSession -> error', error);
});
}
function configureR3() {
try {
return getSession().then(function(session) {
window.R3_COMMON = new r3_common();
window.R3_COMMON.setApiKey('234d6148cb7fc82c');
window.R3_COMMON.setBaseUrl('https://recs.richrelevance.com/rrserver/');
window.R3_COMMON.setClickthruServer('https://'+window.location.host);
window.R3_COMMON.setSessionId(session.id);
window.R3_COMMON.setUserId(session.namespaces.profile ? session.namespaces.profile.id.value : '');
return Promise.resolve();
});
} catch (error) {
console.error('GTM -> configureR3 -> error', error);
}
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
window.R3_HOME = new r3_home();
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Home -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
if (window.location.pathname.match('orderPlaced')) {
configureR3().then(function() {
var orderData = window.dataLayer.find(function(layer) { return layer.orderGroup });
window.R3_PURCHASED = new r3_purchased();
window.R3_PURCHASED.setOrderNumber(orderData.orderGroup);
orderData.transactionProducts.forEach(function(product) {
window.R3_PURCHASED.addItemIdPriceQuantity(
product.id,
product.sellingPrice,
product.quantity,
product.sku
);
});
rr_flush_onload();
r3();
});
}
} catch (error) {
console.error('GTM -> RR Order placed -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
var productCategoryId = window.dataLayer.find(function(layer) {
return layer.productCategoryId;
}).productCategoryId;
window.R3_COMMON.addCategoryHintId(productCategoryId);
window.R3_ITEM = new r3_item();
window.R3_ITEM.setId({{CAD-DLV-PRODUCT ID}});
window.R3_ITEM.setName({{CAD-DLV-PRODUCT NAME}});
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Product -> error', error);
}
</script>
<script charset="utf-8" type="text/javascript" async>
try {
configureR3().then(function() {
window.R3_SEARCH = new r3_search();
window.R3_SEARCH.setTerms({{CAD-DLV-TERMO BUSCADO}});
var dataLayerProducts = {{CAD-DLV-SHELF PRODUCT IDS}};
var i = 0;
for (; i < 15; i++) {
if (!dataLayerProducts[i]) break;
window.R3_SEARCH.addItemId(dataLayerProducts[i]);
}
rr_flush_onload();
r3();
});
} catch (error) {
console.error('GTM -> RR Search -> error', error);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment