Anytime you have a performance issue and a check engine light it’s normally good practice to address the check engine light first. I’m not saying that the O2 sensor is causing the problem but it might not be a bad idea to address that first and see if the problem still occurs.