Just like people need to go to Middle and High School before taking on a Masters program, dogs need to learn to do their behaviors in gradually increasing levels of distraction and emotion.
Here's a simple way to build up your dog's ability to do his behaviors for his training and behavior program:
Teach the behavior at home with no distractions around first.
Once your dog is doing great, start practicing in another room and in the backyard.
Once your dog is doing great there, add in the behavior on walks when it's quiet or at home when things are busy!
Last, work on your dog using the behavior in their target environment! Around other dogs and people on walks and/or big distractions like guests being over.