Almost anything we manage to do three times in a row is called a hat trick today. The one thing you can be fairly sure of is that it seldom, if ever, involves a hat. However, a hundred years ago a hat gave rise to the expression. In those days, hat tricks only occurred in game of cricket.
Back in the 1880s, a bowler who took three wickets with three balls bowled one after the other was given a new hat by his club. The feat became known as a hat trick and, as it caught on, people began applying the term to all sorts of other sports & activities.