When Tom Pitfield talks about the campaign of Justin Trudeau, who was recently elected Canada's prime minister, he gets animated.

Trudeau's Liberal Party could not afford a lot of TV time and spent much of its advertising budget on social media. That proved an inspired choice.

"We would create an ad, see how people reacted to it on Facebook, tweak the content and test it again. On some days we would produce more than 50 different ads," said Pitfield, who was in charge of the campaign's digital side. This rapid feedback, he said, allowed his team to offer much more flexible and targeted messages than the competition.

Even Barack Obama's first presidential campaign in 2008 was widely hailed as "data-driven." But it was only in 2012 that his team systematically used digital technology to deal with every campaign's biggest challenge: how to make the best use of a limited budget to reach the right voters. In recent years it has become possible to target voters individually, thanks to the availability of ever more data as well as ever cheaper computing power and better methods to mine them.

To find out where to concentrate its resources, the Obama campaign used polls and other data to generate a statistical model of the attributes potential Obama supporters had in common. "When volunteers knocked on doors in 2008, four out of 10 people they met backed Obama. In 2012 the ratio was nine out of 10," said Dan Wagner, who led Obama's data-science team during his second campaign.

Wagner and his colleagues also pioneered a number of other methods of persuasion in that election. They tested the subject lines of fundraising e-mails ("I will be outspent" raised $2.6 million; "Do this for Michelle" only about $700,000). They found out whether a group of voters they wanted to target watched certain cable shows, which allowed them to use TV advertising more cost-effectively. They tried to identify voters who didn't support Obama who might be persuaded to change their mind (with limited success).

The starting point for all this information-gathering was the Florida recount after the presidential election of 2000, which became necessary partly because of incomplete voter rolls. To avoid a repeat, Congress in 2002 passed the Help America Vote Act (HAVA), which required states to maintain a "single, uniform, official, centralized, interactive computerized statewide voter registration list." This was a bigger deal than it appeared at first sight. Along with improvements in database technologies, HAVA made it possible for the first time for political parties to compile an up-to-date list of all voters in the country. In his book, "Hacking the Electorate," Eitan Hersh of Yale University argued that this laid the groundwork for individual targeting. Campaigners were able to identify voters easily and link them to other public information.

The legislation also kicked off what might be called "database politics." Democrats were the pioneers: In 2006 party officials set up a company called Catalist, which today offers one of the most comprehensive databases on Americans of voting age, covering more than 240 million people. Apart from the official register of voters, it includes other public records and information from commercial data brokers. Each entry contains hundreds of pieces of information, from race to the probability of owning an SUV.

Catalist is best understood as a "data cooperative" for Democratic campaigns, trade unions and other left-wing organizations, said Laura Quinn, its chief executive. For a fee, it gives clients access to its common data pool. They can combine it with their own information and benefit from the firm's analytics expertise.

But the Democratic Party did not want to rely on an outside database. When Obama became president, it decided to create its own. Called VoteBuilder, it also relies on the principle of sharing data. During the primaries all competing Democratic campaigns can use VoteBuilder and combine it with data they gather.

The Republicans' central database, Voter Vault, has commanded less cooperation and has often been neglected between campaigns. Warring factions have insisted on producing their own databases, often working with commercial vendors. After Mitt Romney lost to Obama in 2012, Charles and David Koch, billionaire brothers with a passion for conservative causes, invested millions in i360, a for-profit firm that competes with Voter Vault.

On both sides, access to all these databases can be a highly political issue. Insurgent Democratic candidates in state and local races frequently complain that they are being excluded; they need to show a minimum of cooperation to get the data.

The big question is whether the use of such databases and algorithms changes the outcome of elections. Recent estimates suggest they can add between 2 and 3 percentage points to a candidate's result. In a closely fought election that could be critical, but in an emotionally charged race between, say, Donald Trump and Hillary Clinton it may not play a decisive role.

And although social media give politicians direct access to their voters, big parties still enjoy an advantage because they have the money to hire technical talent, pay for polls and buy advertising on Facebook.

Copyright 2013 The Economist Newspaper Limited, London. All Rights Reserved. Reprinted with permission.