I think I figured out how to tell if your terminal is iTerm2. It needs testing before wide deployment on different platforms.
The problem has always been that environment variables don't get passed through and almost all terminals don't support an escape sequence to report version number. Commit cc8436b adds support for a new escape sequence that reports the terminal name and version number that should be ignored by all other terminals. The trick is you can't just ask for the version and then read it back because that'll hang indefinitely if the terminal doesn't support the new sequence. Instead, you send the custom sequence and then a standard "device status report" which every terminal supports. Then you read back one response, and if it's a DSR response you quit, knowing it's not iTerm2. Otherwise, read both responses.
Commit 95c439f44a77e2419c73e2a44e7f0b423ed37368 adds a shell script that tests if the current terminal is iTerm2 and optionally checks if its version number is greater than or equal to one you specify.
It will only work on versions of iTerm2 that support this new escape sequence, starting with tomorrow's nightly build (2.9.20160304).