Every time when wordpress updates, I have to add tracking code again. What should I do to avoid this issue?
I don't really know, but this one seems to be the most popular: https://wordpress.org/plugins/google-analytics-for-wordpress/
Where are you adding the tracking code? It sounds like it is disappearing after updating the theme (as opposed to the WP core). If you are editing the theme, and you are comfortable with editing the theme files (such as header.php - which I think you might already be doing anyway) then you could add the code there, BUT after creating a child theme. For example, if you are using TwentyFifteen as your theme, you can create a child-theme for TwentyFifteen, edit the header.php there and add in your GA code, and then further theme updates won't wipe your code out. On the other hand, if you are not comfortable with editing the code in header.php, using a plugin as PoPSiCLe suggested is the way to go. I just prefer to install as few plugins as I need to. Sometimes the theme itself will cater for adding in GA code. Sometimes another plugin you might already be using caters for adding in GA code.