Created
August 17, 2016 14:55
-
-
Save oarashi/687ef62416ddf172330d207ec950acd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(){if(typeof talkable == "undefined"){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src="//d2jjzw81hqbuqv.cloudfront.net/integration/talkable-2.2.9.min.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}}(); | |
window._talkableq = window._talkableq || []; | |
window._talkableq.push(['init', {site_id: "into-the-am"}]); | |
var isCurrentPath = function(paths) { | |
for (var i = 0; i < paths.length; i++) | |
if(document.location.pathname.indexOf(paths[i]) > -1) | |
return true; | |
return false; | |
}; | |
//Post Purchase | |
if (Shopify && Shopify.checkout) { | |
var _talkable_checkout = Shopify.checkout, | |
_talkable_order_items = []; | |
for (var idx in _talkable_checkout.line_items) { | |
line = _talkable_checkout.line_items[idx]; | |
_talkable_order_items.push({ | |
product_id: line.sku || line.product_id, | |
price: line.price, | |
quantity: line.quantity, | |
title: line.title | |
}); | |
} | |
var _talkable_data = { | |
campaign_template: 'post-purchase', | |
purchase: { | |
order_number: _talkable_checkout.order_id, | |
order_date: _talkable_checkout.created_at, | |
subtotal: _talkable_checkout.total_price, | |
coupon_code: _talkable_checkout.discount ? _talkable_checkout.discount.code : null, | |
items: _talkable_order_items | |
} | |
}; | |
if (_talkable_checkout.shipping_address) { | |
var shipping_address = _talkable_checkout.shipping_address, | |
shipping_fields = ['address1', 'address2', 'city', 'province', 'zip', 'country'], | |
address = []; | |
for (var idx in shipping_fields) { | |
var address_key = shipping_fields[idx]; | |
if (shipping_address[address_key]) | |
address.push(shipping_address[address_key]); | |
} | |
if (shipping_address.zip) | |
_talkable_data.purchase.shipping_zip = shipping_address.zip; | |
if (address.length) | |
_talkable_data.purchase.shipping_address = address.join(', '); | |
} | |
_talkableq.push(["authenticate_customer", { email: _talkable_checkout.email }]); | |
_talkableq.push(['register_purchase', _talkable_data]); | |
} | |
//Standalone | |
var i = 0; | |
var _timer = setInterval(function() { | |
if(window.talkable) { | |
clearInterval(_timer); | |
runSA(); | |
} else if(i>75) { //max 15s | |
clearInterval(_timer); | |
} | |
i++; | |
}, 200); | |
var runSA = function() { | |
window.talkable.domReady(function() { | |
if (document.getElementById('talkable-invite')) { | |
_talkableq.push(['register_affiliate', {campaign_template:'invite'}]); | |
} else if (document.getElementById('talkable-dashboard')) { | |
_talkableq.push(['register_affiliate', {campaign_template:'dashboard'}]); | |
} else { | |
if ( isCurrentPath(['/cart', '/no-mans-sky-limited-edition-ps4-bundle-giveaway', '/hc/en-us']) | |
) { | |
return; | |
} | |
_talkableq.push(['register_affiliate', {campaign_template:'popup-trigger'}]); | |
//Auto apply coupon code | |
_talkableq.push(['gleam_reward', { | |
callback: function(coupon) { | |
if (window.jQuery) { | |
$("body").on("submit", "form[action='/cart']", function(data) { | |
$('<input />').attr('type', 'hidden') | |
.attr('name', "discount") | |
.attr('value', coupon) | |
.appendTo($("form[action='/cart']")); | |
return true; | |
}); | |
} else { | |
var forms = document.getElementsByTagName('form'), | |
discount = document.createElement('input'); | |
discount.type = 'hidden'; | |
discount.name = 'discount'; | |
discount.value = coupon; | |
for (var i = 0; i < forms.length; i++) { | |
if (forms[i].action.indexOf('/cart') !== -1) { | |
forms[i].appendChild(discount); | |
} | |
} | |
} | |
} | |
}]); | |
} | |
}); | |
}; | |
//hide gleam | |
talkable.subscribe("offer_loaded", "talkable-popup-trigger-widget-iframe", function(data, iframe){ | |
if (!data.gleam_reward) return; | |
if (localStorage.getItem("talkable_hide_gleam") == data.gleam_reward) { | |
var talkable_popup = document.getElementById("talkable-popup-trigger-widget"); | |
if (talkable_popup) talkable_popup.style.display = "none"; | |
} else { | |
localStorage.setItem("talkable_hide_gleam", data.gleam_reward); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment