Politicians invest heavily in social media to amplify narratives about their nations, but the effectiveness of such approaches remains unclear. Analyzing 758,222 posts from US and UK politicians on X (formerly Twitter), we found that right-wing politicians’ posts portraying the nation as exceptional and entitled (defensive identity rhetoric) received 42% more likes and 34% more reposts than their other posts. Left-wing politicians did not enjoy similar benefits online, and Democrats who used more defensive rhetoric (+1SD) in their posts before an election received a 42% smaller vote share. Defensive rhetoric did not hamper the electoral success of Republicans. Posts highlighting national pride and attachment (positive identity rhetoric) benefitted both sides. They received 27% more likes than other posts and politicians who used more positive identity rhetoric (+1SD) before an election received a 16% greater vote share. The research establishes a link between identity rhetoric, online attention, and electoral success.