Reference

page.changeTemplate:before

$page, $template

Example

/site/config/config.php
return [
    'hooks' => [
        'page.changeTemplate:before' => function ($page, $template) {
            // your code goes here
        }
    ]
]