If you are handy with a soldering iron, it is best to directly solder the wires to the sensor and connect it to a locking connector a few inches away. You have to remove the existing connector on the IR sensor in order to thru mount the wires into the connector plated thru holes. Once you connect to your harness, put kapton tape around the mating connectors to prevent accidental disconnects.
If you are not handy, I suggest adding some flexible but strong glue like E6000 around the joint to keep it in place. I wouldn't use a glue gun since it is not as strong as E6000.