How to use JavaScript and Google Analytics to record clicked links in your Harlowe game

Have you ever wanted to know:

  • How users were playing your twine game?
  • How many people visited a specific passage?
  • How many people got to any ending?
  • Where the most people quit playing?

I figured out how to use Google Analytics, Google’s free website analytics tool, to record passage clicks and let you use the Google Analytics panel to look at some of these stats.

In order to use this script, you need to have the following things:

  • Your Twine game hosted at a domain you have control over
  • A Google Analytics account connected to that domain

Setting those things up is beyond the scope of this post.

Finally, you need some JavaScript in your Twine.

Important note: I have never been able to get this working by putting it in the Story Javascript section. I put this code at the beginning of the starting passage.

It has two parts…

  1. The standard Google Analytics code, which google provides
  2. My custom event code that listens for someone to click on any kind of passage link, and records the click.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=[YOUR GOOGLE ANALYTICS ID HERE]"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '[YOUR GOOGLE ANALYTICS ID HERE]');
// End of google-specific code ^

// This is the custom Event Listener that will allow Twine
// to record your users' passage clicks
document.addEventListener('click', function (event) {
	if (event.target.matches('tw-link')) {	
		console.log("A tw-link was clicked");	
		var linkText = event.target.textContent;
		console.log("Link text is");
		console.log(linkText);
		gtag('event', 'Navigation', {
		  	'event_label': linkText,
		  	'event_category': 'LinkTextClicked',
		});					
	}
}, false);  
</script>

Now, once this is uploaded to your site, you should be able to click on a few passages, go into Google Analytics, and see what links were clicked. Note that you can NOT see what SPECIFIC users clicked, but you will still be able to get a sense of how many users there are, and how then what percentage of those users made it to specific sections of the site.

I hope this is useful for someone. Please let me know if you have any questions.

Any use of analytics will likely to need to be tailored the specific story format in use, because of differences in the engines.

In particular, your example is tailored to Harlowe, because of its use of elements unique to Harlowe—i.e., tw-link.

As to why your example didn’t work in Story JavaScript, that would be because it’s HTML. Yes the second <script> tag contains JavaScript, but that’s irrelevant.

Hey, @TheMadExile thanks for the corrections. I forgot about that, and I altered the post to include that it’s Harlowe-specific.

As for the HTML vs. JavaScript distinction: yes, I’m aware there are script tags. When I tried using this code in the “Story JavaScript” section I did not include the script tags and still wasn’t able to get it to work.

How did you load Google’s code?

Truthfully, I do not recall, but I think I just the above code in full without the script tags. Do you have a good modification for doing it in the js section?