Once you are outside, take the dog directly to the area where you want it to go. It may be that your dog gets dirty inappropriately because he suffers from separation anxiety when you leave the house. You should gradually train your dog to be alone, as this will not come naturally to such a social animal. Start by leaving the house for just a few seconds, then go back and extend the amount of time you are away with time.
You can use pheromone supplements recommended by your veterinarian to keep your pet calm while you're away. You can also give him a candy dispenser toy before leaving to make sure he stays busy for some time. In addition, if you punish your dog, he may start to avoid defecating in front of you, even during walks, because he is afraid of your punishment.