Facebook seems to have figured out--for now at least--the holy grail for all media right now: how to make money selling mobile ads.

The social network reported $791 million in profit, more than twice what it earned during the same quarter last year; revenues were up 61 percent to $2.91 billion. That handily beat analyst expectations, and sent the stock up nearly 5 percent in after-hours trading from is market-close price of $71.29.

"We had a good second quarter," said Mark Zuckerberg, Facebook founder and chief executive. "Our community has continued to grow, and we see a lot of opportunity ahead as we connect the rest of the world."

What is Facebook's advantage? The company, as we know, has reams of data to ensure that the ads that appear on your mobile device fit your interests and tastes. It runs a massive service that even a decade years after its founding, more than a billion people are still using every day. And more of those people are logging in on their mobile devices.

That jump in mobile users used to be a problem for Facebook. In fact, it was a major concern when the company headed into its initial public offering two years ago since--as many tech and media companies have discovered--it's more difficult to make money off of small, mobile ads than it is from desktop advertisements.

Those concerns prompted Facebook in 2012 to focus on building up its advantage in mobile advertising, an area long ruled by Google. In the beginning of 2012, Facebook had no mobile advertising revenues. By the end of that year, the company accounted for 5.4 percent of the global mobile advertising market, according to the advertising analysis firm eMarketer. And it's only continued to grow.

This year, analysts project,  that it's primed to take 21.7 percent of all mobile ad spending. Facebook, perhaps faster than any other technology company, has made significant inroads into the mobile ad market and now makes well over half of its revenue from ads served to smartphones and tablets. Facebook has also begun focusing on how to reach people even when they're not on the Web, announcing earlier this month that it has seen early success in its efforts to drive people directly from Facebook's app into other apps, and vice versa.

The company has also begun incorporating information from sites that users visit outside of its network into its own ad platform, increasing the network's vast stores of data and allowing Facebook to tailor ads even more carefully to users. This makes each Facebook user more lucrative for the company--critical since the company is approaching a point where it will literally run out of people on the planet to add.

The pursuit of that data has landed the company in hot water in the past. Most recently, the company ran into trouble with users after some Facebook researchers disclosed that they had used the social network to run a mood experiment on its users.

Still, the number of people using Facebook continues to grow. Facebook reported that it now has 1.32 billion users who log in at least once per month across the globe, up 15 percent from the same time last year. But it also reported that many more users are making Facebook a daily habit -- 829 million people sign into the service every day, up 19 percent from last year.

Facebook saw particularly strong in Asia this past quarter, where it faces stronger competition from local social networks and messaging apps such as WeChat or Line. Facebook's pending acquisition of WhatsApp, a messaging company with a strong international following, is expected to help continue that overseas growth.