Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="index.js"></script>
<script>
let model = new CPT();
// GET This data from Google Analytics
let data = [
['/previous-page1', '/current-page1', '/next-slow-page1'],
['/previous-page2', '/current-page2', '/next-slow-page2'],
['/previous-page3', '/current-page3', '/next-slow-page1'],
['/previous-page4', '/current-page1', '/next-slow-page2'],
['/previous-page2', '/current-page3', '/next-slow-page3'],
['/previous-page3', '/current-page2', '/next-slow-page4'],
['/previous-page4', '/current-page1', '/next-slow-page1'],
];
model.train(
data // Training Data
);
// replace place holder values for commented lines to test with real data
let previous_page = '/previous-page2';//document.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '');
let current_page = '/current-page3';//document.location.pathname;
let target = [
[previous_page, current_page]
];
let predictions = model.predict(
target, // Test input
2, // The number of last elements that will be used
// to find similar sequences, (default: target.length)
1 // The number of predictions required.
);
//This part inserts the browser hint dynamically based on the next page predicted
document.write(`<link rel="prefetch" href="${predictions}">`)
console.log(predictions)
</script>
</head>
<body>
Testing Prefetch
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.