-
-
Save goranseric/26d66754f9be0e7961d3 to your computer and use it in GitHub Desktop.
Regular Expression snippets to validate Google Analytics tracking code (in PHP, JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First, I don't get the exact pattern from google explanation at http://code.google.com/apis/analytics/docs/concepts/gaConceptsAccounts.html#webProperty so I just make some assumptions here (please correct if mistaken): | |
- general pattern must be: UA-xxxx-yy | |
- case-insensitive chars (so it could be 'ua', 'uA', whatever) | |
- x (account number) must be a number and could be in range of 4-9 digits | |
- y (profile within account) must be a number and could be in range of 1-4 digits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Regular Expression snippets to validate Google Analytics tracking code | |
* see http://code.google.com/apis/analytics/docs/concepts/gaConceptsAccounts.html#webProperty | |
* | |
* @author Faisalman <[email protected]> | |
* @license http://www.opensource.org/licenses/mit-license.php | |
* @link http://gist.github.com/faisalman | |
* @param str string to be validated | |
* @return Boolean | |
*/ | |
function isAnalytics(str){ | |
return (/^ua-\d{4,9}-\d{1,4}$/i).test(str.toString()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Regular Expression snippet to validate Google Analytics tracking code | |
* see http://code.google.com/apis/analytics/docs/concepts/gaConceptsAccounts.html#webProperty | |
* | |
* @author Faisalman <[email protected]> | |
* @license http://www.opensource.org/licenses/mit-license.php | |
* @link http://gist.github.com/faisalman | |
* @param $str string to be validated | |
* @return Boolean | |
*/ | |
function isAnalytics($str){ | |
return preg_match(/^ua-\d{4,9}-\d{1,4}$/i, strval($str)) ? true : false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment