Google owns video site YouTube
Google must divulge the viewing habits of every user who has ever watched any video on YouTube, a US court has ruled.
The ruling comes as part of Google's legal battle with Viacom over allegations of copyright infringement.
Digital rights group the Electronic Frontier Foundation (EFF) called the ruling a "set-back to privacy rights".
The viewing log, which will be handed to Viacom, contains the log-in ID of users, the computer IP address (online identifier) and video clip details.
While the legal battle between the two firms is being contested in the US, it is thought the ruling will apply to YouTube users and their viewing habits everywhere.
Viacom, which owns MTV and Paramount Pictures, has alleged that YouTube is guilty of massive copyright infringement.
The UK's Premier League association is also seeking class action status with Viacom on the issue, alleging YouTube, which was bought by Google in 2006, has been used to watch football highlights.
When it initiated legal action in March 2007 Viacom said it had identified about 160,000 unauthorised clips of its programmes on the website, which had been viewed more than 1.5 billion times.
Following the launch of its billion-dollar lawsuit, YouTube introduced filtering tools in an effort to prevent content that infringes copyright from appearing on the site.
The US court declined Viacom's request that Google be forced to hand over the source code of YouTube, saying it was a "trade secret" that should not be disclosed.
But it said privacy concerns expressed by Google about handing over the log were "speculative".
Google's senior litigation counsel Catherine Lacavera said in a statement: "We are disappointed the court granted Viacom's over-reaching demand for viewing history.
"We will ask Viacom to respect users' privacy and allow us to anonymise the logs before producing them under the court's order."
The ruling will see the viewing habits of millions of YouTube users given to Viacom, totalling more than 12 terabytes of data.
Viacom said it wanted the data to "compare the attractiveness of allegedly infringing video with that of non-infringing videos."
YouTube and Google had "compelled" it to go to court, Viacom said, "by continuing to defend their illegal and irresponsible conduct and profiting from copyright infringement, when they could be implementing the safe and legal user generated content experience they promise".
It said it would not be asking for any "personally identifiable information" of any user.
"Any information that we or our outside advisors obtain will be used exclusively for the purpose of proving our case against You Tube and Google (and) will be handled subject to a court protective order and in a highly confidential manner."
Leading privacy expert Simon Davies told BBC News that the privacy of millions of YouTube users was threatened.
He said: "The chickens have come home to roost for Google.
"Their arrogance and refusal to listen to friendly advice has resulted in the privacy of tens of millions being placed under threat."
Mr Davies said privacy campaigners had warned Google for years that IP addresses were personally identifiable information.
Google pledged last year to anonymise IP addresses for search information but it has said nothing about YouTube data.
Mr Davies said: "Governments and organisations are realising that companies like Google have a warehouse full of data. And while that data is stored it is under threat of being used and putting privacy in danger."
The EFF said: "The Court's erroneous ruling is a set-back to privacy rights, and will allow Viacom to see what you are watching on YouTube.
"We urge Viacom to back off this overbroad request and Google to take all steps necessary to challenge this order and protect the rights of its users."
The body said the ruling was also potentially unlawful because the log data did contain personally identifiable data.
The court also ruled that Google disclose to Viacom the details of all videos that have been removed from the site for any reason.