Change back button href in controller?

I have a page that have 3 level, when my user reached the last page (3rd level) I want to change the back button so that the user will go back the first level if clicked. Because by default it will go the second page.

//what should I do before or after this code?
$state.go(‘app.mythirdPage’);

Inject $ionicHistory inside the controller of that view and use $ionicHistory.goBack(-2) to go back two views.

http://ionicframework.com/docs/api/service/$ionicHistory/

I don’t want to go back programmatically, I want to set the back button’s target when user clicked on it.