The pronunciation of the Dutch 'g' depends on the one hand on its place within the word and the adjacent letter(s)*, on the other hand on the speaker's origin (as you go southwards, the 'g' becomes generally softer).
* E.g. in 'mening' the 'g' is quite soft, in 'weg' it's much harder (kind of 'chr').
I don't know if there is a rule but both in your example and in that of mine the softly pronounced 'g' is coming directly after an 'n'. By the way, when I learn a new word I learn also its correct pronunciation. On woorden.org you can find each word's pronunciation.
Hebben is hebben, maar krijgen is de kunst.