We have all visited websites and seen advertising that eerily matches searches we have done or pages we have visited. Sometimes it’s as if our computers actually know what you have purchased or are interested in purchasing, without you telling anyone. With the recent disclosures of Facebook’s data issues, people are more interested than ever to understand the ins and outs of ad targeting. So how does Facebook and other sites learn so much about us?

One way is through our purchase behavior. You walk into CVS and buy Kleenex and Afrin. The next day you start seeing ads for Sudafed in your Facebook Newsfeed. How does this happen? You may have keyed in your phone number at the store, so you could get loyalty points. Information about the contents of your shopping is gathered by a third-party data collector—likely Nielsen-Catalina Solutions— the people who provide all those coupons at the supermarket checkout. They gather that information and sell it. With the use of the information from your loyalty cards, the makers of Sudafed then buy advertising on Facebook and match the buy to purchase data, using name, email addresses etc. Viola, you are now being targeted.

What could be better than your purchase history? Location, location, location. Did you stop by a shop? Are you close to an advertiser’s stores? Here’s a coupon! Advertisers are using all sorts of location signals—your phone’s GPS, Wi-Fi access points around you, IP addresses, etc.— to follow your breadcrumbs. Other apps can pinpoint your location and serve you ads back through Facebook. Apps will use your iPhone’s Identifier for Advertisers (IDFA), a number stored on your iPhone, to match up any other history associated with your IDFA, including your Facebook account.

Of course, there’s another way Facebook knows, pretty much everything about you: your web browsing history. Facebook Pixel is installed on millions of websites and apps, enabling advertisers and Facebook to see what you do on the web. It’s why you may see an ad for a spatula after browsing spatulas. Add something to a shopping cart? Click on a different product or article on the site? Pixel is in the know.

All that information, combined with your activity on Facebook and Instagram—which pages or posts you’ve liked, the people you are friends with and more—gives the social networking conglomerate a very good portrait of you. The portrait gets clearer with even more information from data brokers: your salary, car preference, home size, political affiliations, spending habits and far more. It’s what allows any advertiser to log into Facebook Ads Manager and start targeting.

There are ways to inhibit much of this data collection. Don’t use loyalty cards or register them to phone numbers and email addresses you don’t actively use. Go into your smartphones’ Settings and turn off location tracking and disable location history. While in Settings, limit Ad Tracking or Ad Personalization. However, short of deleting Facebook and other apps from your phone and desktop, a certain amount of information will get out. And that is not all bad. The point of targeted advertising is to provide you with information for products and services you may really want, instead of random messaging. When we receive information of value to us it is not intrusive, instead it becomes valuable. Isn’t that what all advertising should be?