Last active
August 29, 2015 14:16
-
-
Save aripalo/e16f2d3f17a98afd01f8 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>WhatsApp jQuery esimerkki</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- Google Analytics skripti --> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-34074683-1', 'auto'); | |
ga('send', 'pageview'); | |
</script> | |
<style> | |
body { | |
background-color: grey; | |
} | |
.wa-button { | |
width: 200px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>jQuery esimerkki whatsapp linkin generoinnista</h1> | |
<!-- elementti jonka sisään whatsapp painike sijoitetaan --> | |
<div id="button-parent-example"></div> | |
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> | |
<script> | |
// Huom. vaatii jQueryn | |
(function($){ | |
$(document).ready(function(){ | |
// Parent-elementti johon whatsapp painike luodoon | |
var $buttonParent = $('#button-parent-example'); | |
// URI enkoodatut title & url | |
// - koska title:ssä yleensä aina välilyöntejä | |
// - url:ssa ... noh... urli komponentteja | |
var title = encodeURIComponent(document.title); | |
var url = encodeURIComponent(document.URL); | |
// Luodaan Whatsapp painikkeen sisältö | |
var $buttonContent = $('<a class="wa-share" title="Jaa WhatsApp:ssa!" href=whatsapp://send?text=' + title + '%20' + url + '%3Futm_source%3Dwhatsapp%26utm_medium%3Dartikkeli%26utm_campaign%3Dwhatsapp-share' + '>WhatsApp!</a>'); | |
// Asetetaan click eventin kuuntelu painikkeeseen | |
$buttonContent.on('click', function(){ | |
// Lähetetään Universal Analytics Social Interaction Event | |
window.ga('send', { | |
'hitType': 'social', | |
'socialNetwork': 'whatsapp', | |
'socialAction': 'share', | |
'socialTarget': document.URL, // Kohde osoite | |
'hitCallback': function() { // Callback testataksemme, että Send lähtee. (Ei pakollinen) | |
if(window.console) { | |
console.log("Send suoritettu. Katso UA raporteilta kohdasta Acquisition > Social > Plugins"); | |
} | |
} | |
}); | |
}); | |
// sijoitetaan painike paikalleen | |
$buttonParent.append($buttonContent); | |
}); | |
}(window.jQuery)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment