John Davidson

php - Adding functionality to regular expression code for finding analytics tracking id

0 comments
Message:


I have a regular expression function that will crawl a webpage and tell me if a Google Analytics code is on the site:


 function checkUA($domain) {
$input = file_get_contents($domain);
if ( $input !== false ){
//regex to check for UA or GA4 code that returns all matches
$regex = '/\b[A-Z][A-Z0-9]?-[A-Z0-9]{4,10}(?:\-[1-9]\d{0,3})?\b/';
//if there is a match, return a unique array (tracking may be included more than once)
if(preg_match_all($regex, $input,$matches)){
return array_unique($matches[0]);
}else{
//if no match is found, let us know
return 'no match found';
}
}else{
return 'Site is blocked from crawlers';
}
}

This function will find any tracking IDs that start with UA YT MO G DC AW:


UA-12345-1 G-J2DV45G DC-JGWWE32 AW-GER322


I'm trying to add functionality to the regular expression to find if there are Google Tag manager IDs as well:


 $regex = '/\b[A-Z][A-Z0-9]?-[A-Z0-9]{4,10}(?:\-[1-9]\d{0,3})?\b/';

So an ID that looks like this: GTM-5TDMDSZ


I can't seem to figure out how to add a check into my regular expression above that will also include checking for GTM IDs like the one above.



My Answer:
You can modify your regular expression to include the Google Tag Manager IDs by adding an OR condition using the pipe symbol (|). Here's how you can update your regular expression:

php
$regex = '/\b(?:UA|G|DC|AW|GTM)-[A-Z0-9]{4,10}(?:-[1-9]\d{0,3})?\b/';


In this updated regular expression, `(?:UA|G|DC|AW|GTM)` will match either UA, G, DC, AW, or GTM followed by a hyphen and then `[A-Z0-9]{4,10}` will match 4 to 10 characters of either uppercase letters or numbers. The rest of the regular expression remains the same.

With this modification, your function should now be able to find both Google Analytics tracking IDs and Google Tag Manager IDs on the webpage.

Rate this post

3 of 5 based on 8814 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us