The mayor's office in Colmenar Viejo, a historical town north of Madrid, said fines and warning signs had failed to coax some owners into picking up their dogs' mess.
So from next week dog-walkers will be spied on by a professional "Canine Detective" who will prowl the town to catch negligent dog owners red-handed.
"This person, incognito, will watch the streets and public spaces in which most dog dirt has to be cleaned up," the town hall said in a statement.
"Their job will be to film owners who do not pick up after their dogs in flagrante and hand in this evidence along with a report as proof in police proceedings."
The town hall said offenders would face possible fines of up to €150 ($200), with higher penalties for repeat offenders.
Before the detective starts work, authorities have sent actors into the streets dressed up as detectives to warn locals of the anti-excrement campaign, which it said was motivated by health concerns.
"Most dog owners are responsible people who do not let their pets defecate in inappropriate places and pick up the excrement immediately," said Colmenar Viejo's conservative Mayor Miguel Ángel Santamaría.
"But unfortunately there is a minority that lacks consideration for others and leaves a trace of its incivility in streets, pavements and even the entries to schools or children's playgrounds."