I had a pretty bad situation when my rescue dog started sneaking into a guest bedroom and peeing. We didn't realize it until it was out of hand. We had the carpet professionally cleaned and they used enzymatic cleaner (basically the Nature's Miracle stuff) and that helped, but didn't get rid of the smell completely.
What did work was covering the carpet in baking soda, closing the door and leaving it for 24 hours with an open window, then vacuuming it up. Then we did it again. We might have done it a third time, I don't remember.
That worked.