A red dye is added to diesel fuel (gas oil) for non-road use, to show that tax has not been paid. A similar dye is added to heating oil, as essentially the two fuels are the same, and are interchangeable.
There may be differences in fuel formulation - e.g. road diesel may be mandated to be ultra-low sulphur, whereas the same requirement may not exist for off-road gas oil. Depending on the emission control system, this may cause problems, as catalytic particulate filters may be damaged by sulphur. By and large, however, sulphur plays an important role as a lubricant in a diesel engine, and the transition to ULSD fuel has been very difficult for car manufacturers (e.g. BMW and MB engines from the early 2000s would be rapidly destroyed by the use of ULSD fuel).
The fuel dye has been designed to be easily detectable. It is visibibly detectable even at dilutions of 1:50 or 1:100. Use of optical analytical equipment can detect dilutions of 1:5000.
In some areas, the dye may also be isotopically labelled with Carbon-13 (this isotope is very rare in natural oil) and this allows detection of the dye down to dilutions of 1:100000 using a gas-chromatograph-mass-spectrometer. Additionally, the isotope label is not destroyed by 'laundering' techniques that chemically destroy the dye, or bleach it with UV light.
While the dye does not damage or stain components of the engine, it's ability to be detected in trace amounts mean that, in suspected cases, forensic examination of engine parts (e.g. fuel filter or fuel pump) to extract samples of fuel may be worthwhile, even if tainted fuel is not detected in the main tank.