Whenever you use
Google or
Facebook without being logged in to an account, a cookie is saved to the local system.
It is anonymous at this point in time even though it reveals information about your browsing habits to the company that set it.
Google for instance knows that you have searched for this and that but cannot link the information to you as a person. The company even states that it
uses anonymous identifiers in its privacy policy.
The same is true for Facebook which may notice that you visit sites that display Facebook widgets but cannot link that to an account on the site.
If you decide to sign in to an account afterwards, all previous activities linked to the "anonymous you" are now linked to the user account.
This is the case because the same root domain cookie is being used by Google and Facebook to track anonymous and logged in users.