They do it because it works. Not only does it lead to more direct sales, but the app is scraping data that can be further used to generate sales, either to the user directly or sale of their data to third parties. And people who don’t like their habits being gamified will simply not install the app.
How many people are going to say “I refuse to go to Dunkin because they went too far gamifying their rewards program in their shitty app I never use”? Probably close to zero. So, the cost of doing it is whatever they pay their offshore contractors, who probably can’t afford to come visit the US, much less go to Dunkin for awful coffee every day.
The price of very large publically traded companies is set entirely by supply and demand. Yes, those shares do represent a tiny share of ownership in the company, which means a share in the profits and assets of the company. But there is no law tying them together. Tesla’s market value is so high because there are more buyers than sellers, it’s as simple as that.