Skip to content

Instantly share code, notes, and snippets.

@MatthewCallis
Created September 18, 2015 17:58
Show Gist options
  • Save MatthewCallis/6864176cf6dd344e173e to your computer and use it in GitHub Desktop.
Save MatthewCallis/6864176cf6dd344e173e to your computer and use it in GitHub Desktop.
Defer Loading of Scripts
downloadJSAtOnload = ->
scripts = [
{ src: '//www.googleadservices.com/pagead/conversion.js' } # Google Adwords
{ src: '//platform.twitter.com/oct.js' } # Twitter Conversion Tracking
{ src: "//js.hubspot.com/analytics/#{(Math.ceil(new Date()/300000)*300000)}/123456.js", id: 'hs-analytics' } # Hubspot
{ src: '//tag.perfectaudience.com/serve/SHASHASHASHASHASHA.js' } # Perfect Audience
{ src: '//cdn.bizible.com/_biz-a.js', id: 'bizible-settings' } # Bizible
{ src: "#{if ("https:" is document.location.protocol) then "https://s" else "http://a"}.adroll.com/j/roundtrip.js" } # Adroll
]
for script in scripts
element = document.createElement('script')
element.async = true
element.src = script.src
element.id = script.id if script.id
element.setAttribute('data-attach-secure-forms', true) if script.id is 'bizible-settings'
document.body.appendChild element
# Twitter Conversion Tracking
setTimeout( ->
window.twttr.conversion.trackPid('123ab') if typeof window.twttr isnt 'undefined'
, 500)
# AdRoll
window.__adroll_loaded = true
return
# Google Adwords
google_conversion_id = 1234567890
google_conversion_language = "en_US"
google_conversion_format = "3"
google_conversion_color = "ffffff"
google_conversion_label = "abcdefghijklmnop"
google_conversion_value = 0
# Perfect Audience
window._pa = window._pa or {}
# AdRoll
window.adroll_adv_id = '0123456789ABCDEF012345'
window.adroll_pix_id = '6789ABCDEF0123456789AB'
if window.addEventListener
window.addEventListener 'load', downloadJSAtOnload, false
else if window.attachEvent
window.attachEvent 'onload', downloadJSAtOnload
else
window.onload = downloadJSAtOnload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment