WHEN all else fails, do the right thing. That appears to have been the SNP’s maxim this week after its self-inflicted problems with Cambridge Analytica.

The data firm, which is linked to a privacy breach affecting up to 87m Facebook users, has multiple connections to the Tory party, through personnel and donations.

It has been promiscuous in pitching to almost every UK party.

Yet the SNP, which now appears to have had little more than a glancing contact with the company two years ago, still managed to make headlines for a week over it.

READ MORE: SNP releases email contacts with Cambridge Analytica

Some will doubtless cite this as more evidence of media bias, and a determination by parts of the press to grind out ‘SNP bad’ stories. Some will say that about yours truly.

But what Scotland’s governing party gets up to is always going to be news, especially if it looks like it’s hiding something. And in the second respect, the SNP excelled itself.

When CA became a hot story last month, SNP MPs raced to the summit of Mount Moral and started laying down the law to their morally suspect opposite numbers.

SNP Westminster leader Ian Blackford was particularly strident in demanding answers from Theresa May about links between CA, the Tories and Whitehall departments.

Then a former CA employee told a Westminster committee the firm had been in touch with the SNP too.

Mr Blackford looked daft, SNP HQ shifty and hypocritical.

The party then defaulted to its disastrous lockdown mode. After a grudging statement that a single “external consultant” had met CA, decided they were cowboys and walked away, the party refused to say another word on the subject.

Nicola Sturgeon described this as “complete transparency”. It was actually complete folly. As soon as the party tried to draw a line under the subject, a drip-drip of new information began to wash it away.

READ MORE: Mike Russell refuses to answer key questions about SNP contact with Cambridge Analytica

A couple of months ago, the SNP applied the same “shutters down” approach to Mark McDonald, the former minister accused of sexual harassment. It didn’t work then, and it didn’t work for CA. Instead, it came back to bite the party hard.

It turned out the “external consultant” was not some arm’s length contractor but one of the SNP inner circle, Kirk J Torrance.

A former head of new media at the SNP who set up his own company, he was “external” only in the most lawyerly sense when he met CA in London in February 2016. He was really part of the family at HQ.

Then CA started talking to the media about its contacts with the SNP, suggesting they were more extensive than the party had let on.

The Herald learned the SNP’s head of IT, Chris Jones, had also been in touch with CA ahead of Mr Torrance meeting them.

When the paper tried to get a straight answer from the SNP’s press officer on Tuesday evening about Mr Jones, he complained we were wasting his time and hung up.

It wasn’t all frosty silence though. Sometimes, information was plain wrong. Last week, Ms Sturgeon’s official spokesman said CA went to the SNP first. “My understanding, quite clearly, is that they were approaching the SNP,” he said.

On Wednesday, while MSPs were distracted by a late vote at Holyrood, the SNP released a slew of emails showing Mr Jones had in fact made the first contact with CA.

They also showed he and SNP lawyer Scott Martin had planned to meet the firm in London, although only Mr Torrance went in the end.

And they also suggested that, rather than running a mile from CA, the party had left the door ajar to more contacts months later.

More helpfully for the SNP, the emails showed CA were pushy, foot-in-the-door nuisances who kept badgering the party after the initial contact. The emails also show the SNP largely ignored the company after the London meeting.

The release was the right thing to do. It drew the sting from the issue almost instantly. The SNP deserve credit for coughing up the details.

Mr Blackford was able to reclaim the high ground forfeited by the early lack of candour and call credibly for other parties to follow the SNP’s lead and come clean.

In years gone-by, the SNP would have made a virtue out of early disclosure, rather than holding back what was going to come out anyway.

If the SNP had published all it held on day one, it could have said quickly and simply that it had met CA, discounted them and walked away. Instead, it bungled and concealed and ended up creating a story about its own competence.

“It’s been badly, badly handled,” says a Westminster party source. “There’s a feeling that they [HQ are all over the place. They have people with all these fancy titles and yet the communication is appalling.”

The great irony, I’m told, is the SNP never wanted to work with CA - they wanted to snoop on them.

READ MORE: Second key SNP figure caught up in Cambridge Analytica row

The day before Mr Jones’s “keen to have a chat” email, a Times article was one of the first to report the firm was working with the Leave campaign in the EU referendum.

The pro-Remain SNP naturally wanted to know what tools their rivals were using, so they got in touch with CA and had a shufty.

Opposition research is a standard part of backroom campaigning, so there is nothing out of order in this.

It was rather a smart move. It’s a pity the SNP weren’t smarter in handling the aftermath. I hope they’ll see the merit in transparency hereafter. Now, about that buried Growth Commission report...