Select Page

Shopify integration

Sådan opsætter du integrationen mellem Shopify og Heyloyalty.

Step-by-step guide

Dokumentation

<script>

var HlObject = window.HlObject = {

getProductId: function() {

var productId = ‘{{ product.selected_variant.id }}’;

if (productId == ”) {

{% for variant in product.variants %}

{% if forloop.first %}

var productId = ‘{{ variant.id }}’;

{% endif %}

{% endfor %}

}

return productId;

},

getCategoryId: function() {

{% assign categoryId = product.tags | join: ‘, ‘ %}

return ‘{{ categoryId }}’;

},

getProductsInBasket: function() {

var productsArray = [];

{% for product in cart.items %}

productsArray.push(‘{{ product.variant_id }}’);

{% endfor %}

return productsArray;

},

addEventToAddToCart: function() {

var elements = document.getElementsByName(‘add’);

for (var i = 0; i < elements.length; i++) {

elements[i].addEventListener(‘click’, HlObject.handleAddToBasket, false);

}

},

handleAddToBasket: function(e) {

var form = e.target.closest(‘form’),

productId = form.elements.namedItem(‘id’).value,

amount = (form.elements.namedItem(‘quantity’) != null) ? form.elements.namedItem(‘quantity’).value : 1,

basketId = undefined;

{% if product %}

var categoryId = HlObject.getCategoryId(),

categoryName = (‘{{ product.type}}’ != ”) ? ‘{{ product.type }}’ : undefined;

{% else %}

var categoryId = ”,

categoryName = undefined;

{% endif %}

hlt.addToBasket(productId, amount, categoryId, basketId, categoryName);

},

addEventToRemoveFromCart: function() {

var elements = document.querySelectorAll(‘a[href^=’/cart/change’]’);

for (var i = 0; i < elements.length; i++) {

elements[i].addEventListener(‘click’, HlObject.handleRemoveFromBasket, false);

}

},

handleRemoveFromBasket: function(e) {

var clickTarget = e.target,

elements = document.querySelectorAll(‘a[href^=’/cart/change’]’),

linkTarget;

for (var i = 0; i < elements.length; i++) {

if (elements[i].contains(clickTarget) || elements[i] == clickTarget) {

linkTarget = elements[i];

}

}

if (linkTarget == undefined) {

return;

}

var str = linkTarget.getAttribute(‘href’).match(/line=./)[0],

lineNo = str.split(‘=’)[1] – 1,

products = HlObject.getProductsInBasket();

hlt.removeFromBasket(products[lineNo]);

},

addEventToUpdateCart: function() {

var elements = document.getElementsByName(‘update’);

for (var i = 0; i < elements.length; i++) {

if (elements[i].tagName != ‘A’) {

elements[i].addEventListener(‘click’, HlObject.handleUpdateBasket, false);

}

}

},

handleUpdateBasket: function(e) {

var products = HlObject.getProductsInBasket(),

quantatieInputs = document.getElementsByName(‘updates[]’),

data = [];

for (var i = 0; i < products.length; i++) {

data.push({

productId : products[i],

amount : quantatieInputs[i].value

});

}

hlt.updateBasket(data);

}

};

(function (d, t) {

var g = d.createElement(t),

s = d.getElementsByTagName(t)[0];

h = ‘https:’ == document.location.protocol ? ‘https://’ : ‘http://’;

g.src = h + ‘tracking.heycommerce.dk/hlbi.js’;

s.parentNode.insertBefore(g, s);

} (document, ‘script’));

window.addEventListener(‘load’, function() {

{% if product %}

var productId = HlObject.getProductId(),

categoryId = HlObject.getCategoryId(),

categoryName = (‘{{ product.type}}’ != ”) ? ‘{{ product.type }}’ : undefined;

hlt.visit(productId, categoryId, categoryName);

{% else %}

hlt.heartbeat();

{% endif %}

HlObject.addEventToAddToCart();

if (window.location.href.indexOf(‘/cart’) != -1) {

HlObject.addEventToRemoveFromCart();

HlObject.addEventToUpdateCart();

}

});

document.onsubmit = function(e) {

if (e.target.id == ‘contact_form’) {

// for newsletter signup section

var form = document.getElementById(‘contact_form’),

permission = document.getElementsByName(‘contact[tags]‌‌‌‌’)[0];

if (form.contains(permission) && permission.value == ‘newsletter’) {

var email = document.getElementById(‘Email’).value;

if (email != ”) {

hlt.pairEmail(email);

}

}

}

}

</script>

<script>   

(function (d, t) {        var g = d.createElement(t),           

s = d.getElementsByTagName(t)[0];           

h = ‘https:’ == document.location.protocol ? ‘https://’ : ‘http://’;             

g.src = h + ‘tracking.heycommerce.dk/hlbi.js’;       

s.parentNode.insertBefore(g, s);    } (document, ‘script’));       

window.addEventListener(‘load’, function() {       hlt.purchasedBasket();      

{% if checkout.buyer_accepts_marketing %}          hlt.pairEmail(‘{{checkout.email}}’);       {% endif %}    });

</script>