Log every link click

Twine Version: 2.3.16
Story Format: sugarcube 2.36.1

I would like to log all link clicks, and all button clicks ( to record the players path (click + time) through the game)
Is there a way I could do that by adding some js functionality, or will I just have to do it in every link/button

thanks very much for any help

Cheers

1 Like

Within a <<link>> you can use <<set>>, it allows you yo create either a specific variable each time, or an array that will keep each choice. However, if you’re allowing players to use the back arrow you’ll loose some information here.
Also, there’s a time() function (https://www.motoslave.net/sugarcube/2/docs/#functions-function-time) that should help for the time thing (another array, or nested arrays). Again the use of the back arrow might prove annoying.

If you add this to your JavaScript section:

/* Log Clicks - Start */
$(document).on(":passageend", function () {
	$("#passages a").on("click", function (ev) {
		if (!State.variables.clickLog) {
			State.variables.clickLog = [];
		}
		State.variables.clickLog.push({ passage: State.peek(1).title, type: "link", label: $(this).text(), time: new Date(Date.now()) })
	});
	$("#passages button").on("click", function (ev) {
		if (!State.variables.clickLog) {
			State.variables.clickLog = [];
		}
		State.variables.clickLog.push({ passage: passage(), type: "button", label: $(this).text(), time: new Date(Date.now()) })
	});
});
Save.onSave.add(function(save) {  // Requires SugarCube v2.36.0 or later
	if (State.variables.clickLog) {
		save.state.history[save.state.index].variables.clickLog = State.variables.clickLog;
	}
});
/* Log Clicks - End */

then that will automatically log any links or buttons clicked within the passages (it won’t include UI bar clicks) to the $clickLog variable as an array of objects. Each object in the array has .passage, .type, .label, and .time properties you can use to figure out what was clicked, where, and when. For example, $clickLog[0].label would tell you the label of the first item clicked. The .type property will either be “link” or “button”, so you will be able to tell what was clicked if you have a link and a button with the same label in a passage.

Note: The data on the $clickLog variable may get quite large, so I wouldn’t recommend using this in a game where they may go through hundreds of link and button clicks, since all of that data the history may begin slowing down the saves, loads, and passage transitions.

Hope that helps! :slight_smile:

1 Like

That is exactly what I was looking for, and it teaches me something about events like :passageend and how they might be useful to me.

Thank you very much

1 Like