Skip to content

Commit c19a722

Browse files
author
Cory Wright
committed
add GA event tracking
1 parent 0f23cd9 commit c19a722

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ <h1>Cooking the trifle&hellip;</h1>
4747

4848
<article class="card hidden">
4949
<header>
50-
<h3><a href="#" target="_blank">Cooking the trifle&hellip;</a></h3>
50+
<h3><a href="#" target="_blank" data-ga-action="watch">Cooking the trifle&hellip;</a></h3>
5151
</header>
5252
<!-- <p>
5353
Episode description&hellip;
5454
</p> -->
5555
<footer>
56-
<a class="button" href="#" target="_blank">Watch on Netflix</a>
57-
<button class="warning">Another!</button>
56+
<a class="button" href="#" target="_blank" data-ga-action="watch">Watch on Netflix</a>
57+
<button class="warning" data-ga-action="another">Another!</button>
5858
</footer>
5959
</article>
6060

script.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,30 @@ var getRandomEpisode = function() {
3939
// Update the header
4040
$('h1').text(randomItem(headings));
4141

42+
// Update the elements for data tracking
43+
$('a, button', '.card').data({
44+
episode: episode.episode + ' - ' + episode.title,
45+
season: season.season
46+
});
47+
4248
// TODO: Add episode descriptions to JSON and load them into card
4349
};
4450

51+
// event tracking
52+
var trackCardClick = function($target) {
53+
var action = $target.data('gaAction');
54+
var label = $target.data('episode');
55+
var value = $target.data('season');
56+
var event = {
57+
eventCategory: 'Card Click',
58+
eventAction: action, // button or link action
59+
eventLabel: label, // episode number and name
60+
eventValue: value // season
61+
};
62+
63+
window.ga('send', 'event', event);
64+
};
65+
4566
// I don't love this name. What's better, or is there a better way to do this altogether?
4667
var init = function() {
4768
getRandomEpisode();
@@ -50,6 +71,10 @@ var init = function() {
5071
$('.card.hidden').removeClass('hidden');
5172

5273
// Bind UI handlers
74+
$('a, button', '.card').on('click', function() {
75+
trackCardClick($(this));
76+
});
77+
5378
$('.card button').on('click', function() {
5479
getRandomEpisode();
5580
});

0 commit comments

Comments
 (0)