I am developing a chrome extension that extends the functionality of our company portal. Our company does not own this portal; The portal is maintained and owned by a different company. If we want to change things, we cannot; We must ask said company to implement the change, and its up to them whether they do or not. So like I was saying, the extension does things to improve accessibility and navigation, as well as inserting specific bits of functionality that make it easier to use the website.
I want to make the extension specific to the user type, just like within the portal itself. Within our company portal, we have different roles: agents, admin, brokers, etc. However, there is really only one way for me to detect the type of user: gtag, or a google tag. The portal uses google tags, and I noticed while searching through their html, that there is a google tag for the user type. So my extension could in theory search the html to find the google tag, and extract the value for 'user-type'!
There is a lot of documentation on how to add a google tag to a website, but none to retrieve a google tag. However, I know it is possible. There is an chrome extension (provided by google) called Tag Assistant that does just this. It detects google tags on any page you are on, and then displays them in the extension's pop-out window.
How can I retrieve the 'user-type'
value from a google tag on a page?
Here is the code on the page I am looking at:
<script>
gtag('event', 'page_view', {'user-type': 'admin'});
</script>
For this example, I need to extract the 'user-type'
value of 'admin'
.
I cannot think of anything very ingenious. So far, all I can think of is using jquery and regex patterns to get it. For example, this code accomplishes the task:
$("script:contains('user-type')").text().match(/(?<=('user-type':\s\'))[a-zA-Z]+(?=('))/g)
I guess the I have an answer, but it just is not as elegant as I am thinking it should be.
How you would do it? Or am I overthinking this, and should stick with my current solution?