There is a naive belief at the top of government that because the Tories are only a fart’s yard behind Labour in the polls — despite daily manifestations of schism, scandal and incompetence — everything will turn out fine in the end. But this is to ignore the party’s greatest structural weakness: it is clueless in cyberspace. On the social media battlefield, it is fighting with knitting needles against Labour’s laser-guided missiles.

The crude stats are humiliating for Theresa May. Her Twitter and Facebook accounts have 411,000 and 540,000 followers respectively, compared with 1.6 million and 1.4 million for Jeremy Corbyn. His online films and tweets are seen by millions, — many times the number who hang on the Prime Minister’s digital words. This social media deficit will be a serious problem for whoever leads the Conservative party into the next general election because that is where the marginal voters who determine the outcomes of elections and plebiscites are recruited or alienated. David Cameron’s lame jibe from 2009 that ‘too many tweets make a twat’ is redolent of another era, laying him open to the twin charge of casual sexism and techno-politics Luddism.

What makes the Conservatives’ digital frailties all the more extraordinary was that it was Tory technocrats who won the Brexit referendum. The eureka moment of Vote Leave’s campaign was when director Dominic Cummings decided to deliver a small number of simple messages — mainly that dubious though effective one about Brexit liberating £350 million a week for the NHS — to people shown by data analytics to be the most persuadable. He set up the campaign as if it were a hedge fund staffed by rocket scientists who would be rewarded for finding systems that made the most effective use of the £7 million budget allowed by the rules for wooing voters.

The star performer for Vote Leave was, according to Cummings, its operations director Victoria Woodcock, who now works for the business software company Sage. According to Cummings, Woodcock was the ‘most indispensable person in the campaign; if she’d gone under a bus, Remain would have won… Some people are 50 times more effective than others. She is one of them.’

What she did was to manage the creation of new ‘canvassing software’ — appropriately named after her, as Vics or Voter Intention Collection System. The point was to suck in and process data about voters ‘from social media, online advertising, websites, apps, canvassing, direct mail, polls, online fundraising, activist feedback and… a new way to do polling’. Vote Leave was to use ‘experts in physics and machine learning’ to do ‘proper data science… far beyond the normal skills applied in political campaigns’.

Information about voters was gathered centrally and locally. It was put into Vics, which then generated ‘star ratings so that local teams could target the streets more likely to contain Leave voters’. One especially clever idea was to collect information from a Vote Leave online football competition, nicked from a PR stunt by the billionaire Warren Buffett, who in 2014 offered $1 billion to anyone who could predict the results of all 63 games in a US college basketball tournament. Vote Leave invited contestants to guess the results of all 51 matches in the Euro 2016 tournament, with the chance of winning a whopping £50 million, so long as they filled in their phone number, email address and home address, and rated themselves on a scale of one to five in respect of how likely they were to vote for staying in the EU.

This data was gold: Vote Leave acquired the contact details and voting intentions of hundreds of thousands of people, many of whom would never have participated in conventional surveys. Vics used the information to direct door-knocking and social-media lobbying at those who had revealed they were susceptible to the Brexiteers’ arguments. Because the Leave and Remain votes were roughly even at the start of the campaign, it was Leave’s vast superiority in wooing susceptible don’t-knows via social media that made all the difference.

Now to be clear, neither Labour nor the Tories were as focused or ruthless in their use of social media in the 2017 general election. But Labour came closest, by offering to wipe out the £9,000-a-year fees for students, its equivalent of £350 million for the NHS, and with a smart online campaign to get the vote out. What the crucible of the election also demonstrated was the relative power on Facebook of Corbyn’s ‘authentic’ personality.

According to Labour’s own stats, ‘likes’ of Corbyn’s page rose 31 per cent during the campaign, to 1.2 million, with the number of people he reached every week soaring from under four million to more than 24 million. And his videos were seen 22 million times in the first week of June, up from 2.5 million in April. Which probably means that the Labour leader personally on social media outperformed May in impact by a factor of more than two to one. As for the Labour party as a social-media player in its own name, it also punched through to far more people than did the Tories. According to the social media agency We Are Social, Labour increased its social media following by 61 per cent to 868,000 in the first six weeks of the battle, compared with 6 per cent to 596,000 for the Tories.

Of Corbyn’s followers, a quarter were under 24 and just over half were under 34. That points towards the most astute digital initiatives taken by Labour, which was to encourage young people to register to vote and then more or less walk them to the correct polling station. Astonishingly, Labour and Corbyn’s partnership with stars of grime music was not the car crash suffered by the previous Labour leader, Ed Miliband, when he tried to appear cool, hip and funny — with Russell Brand, for example — in 2015.

Here are the numbers that show why Corbyn interviewed on film in a north London pub by the grime artist JME was not embarrassing dad dancing: via text messages and Snapchat, Labour generated 1.24 million visits to a special webpage the party created from public data and crowdsourced information that would direct people to where they were allowed to vote (more than 675,000 of these visits came from Snapchat). More than a fifth of these were under 24 and three-fifths were under 35. They were a significant contributor to Labour’s surge.

What matters perhaps most is that a majority of us mistrust the established media, and instinctively place greater trust in what friends or contacts share with us on Facebook and Twitter. The act of sharing is a personal endorsement, which lends an article or video greater credibility. Throughout this year’s election, this massively helped Labour: all I could see in my own news feeds were people angry about the government failing to provide adequate resources to schools and hospitals, or furious about its proposal to make old people pay more towards their own social care.

So Labour’s greatest advantage over the Tories may be that it is part of a broader movement, much of it on social media. There are lots of closed Corbyn-supporting Facebook groups that share pretty much anything he puts out. And then there is Momentum, a movement explicitly created to support him.

Its social media activity complemented Labour’s, and the Tories had no equivalent. One of its videos, ‘Daddy, why do you hate me?’, a conversation between a little girl and her dad set in 2030 about why he had voted for Theresa May and ruined her life prospects, was watched 5.4 million times in two days. Momentum also fomented ‘WhatsApp cascades’ on polling day, or messages shared on WhatsApp with an estimated 400,000 young people that were designed to get the vote out. For what it’s worth, all the successful digital media players, Momentum and Vote Leave for example, have noticed that WhatsApp and text messages are vastly more efficient ways of communicating than emails, which tend to be treated by recipients as junk and ignored.

The measure of how far behind the Tories were in technological know-how came on 31 May this year when YouGov published a poll showing the Tories on 42 per cent and Labour on 39 per cent. This was a little over a week before the election, and was remarkably close to the final result. The verdict on Twitter of perhaps the most distinguished of May’s election campaign team, Jim Messina — who had been Barack Obama’s deputy chief of staff and had managed his 2012 re-election campaign — was ‘Spent the day laughing at yet another stupid poll from @yougov’. And Mark Textor, the right-hand person of Sir Lynton Crosby (both of them were at the heart of the May team) concurred that ‘it was quite bizarre’. What they both seemed to have either missed, or underrated, was that YouGov was employing a very similar data-gathering technique to the highly successful model deployed by Vote Leave.

Even if the Tories get their digital act together, and I understand they are frantically trying to hire ‘digital natives’, they have a huge structural disadvantage in that millions of Labour’s supporters are young and living their lives on social media in the way that the Tories are not. This represents not just a weakness for the Tories, but a structural flaw that in a few years could make it almost impossible for them to win an election. They need to recognise that their current debate about the policies and ideas they need to make themselves relevant will be pointless if it takes place outside the digital forum that is reinventing democracy.

In a future not very far away (possibly as soon as the next election), campaigns will be won and lost in the way that hedge funds like the multi-billionaires Jim Simons and Bob Mercer’s Renaissance Technologies have for years beaten the market, that is, with data-gathering and self-learning algorithms. Vote Leave got close with the way they used their Vics programme and the expertise of the specialist social media marketing firm AggregateIQ to gather information about voters and then target their marketing in a more laser-like way. A party’s values and messages matter. But in today’s digital Babel, they are probably less important than how the message is presented and to whom it is communicated.

Politics has become a digital arms race and there are dangers to the integrity of our democracies when one side in a political campaign is deploying massively more effective techniques than the other. The available technologies are now a million miles ahead of the rules we have to enforce the fairness of elections, and the watchdogs do not seem to know what to look for. It is not so much Big Brother who will be bossing us, but Big Data. And for now, at least, the Tories have barely logged on.

WTF, Robert Peston's new book on the crisis in western politics, is out now, published by (Hodder & Stoughton), RRP £20 or £13.63 on Amazon.