Gratitude is a function of mindfulness. When you are mindful and present you can cultivate gratitude for the experience that you are having regardless of what it is. Each experience leads into the next. Thus even the most enjoyable experiences are products of the less enjoyable ones that proceeded them.