Javascript url extractor3/27/2023 ![]() Such cases help me learn as well (since I’m still at the very beginning of my JS journey). ![]() ![]() If you have any edge cases you’d like to find a solution to, drop me a comment below. The split() method is definitely really useful in such tasks when I need to parse a string (usually URL) and fetch a part of it (if the input value always follows the same structure). So that’s about it for this fairly short blog post. This variable will return the value of 123456 if the dlv – productId variable’s value is 123456-887766).Įxtract part of the URL path with Google Tag Manager: Final words This defines a host of utility methods to work with the query string of a URL. In JavaScript, the index number of the first item is 0 (zero), not 1, hence I entered 0. In modern browsers, this has become a lot easier, thanks to the URLSearchParams interface. When split() method created an array of two items ( 123466). I chose the first one.I set the dash ( – ) as a delimiter (separator) because I want to split the product id right where that dash is (reminder: the initial id looks like this 123456-887766).It must be an anonymous function, e.g.This versatile variable must include a script that meets two criterias: In order to extract part of the URL path, we’ll need to use a Custom JS variable. We’ll try to do that step-by-step so that it would be easier to understand for those who are just starting with JavaScript (disclaimer: I also belong to the group of people who are still starting with JS). In order to access the 2nd subdirectory of the URL, we would need to write a little Custom JavaScript. If you’re not sure what the subdirectory in the URL is, a scheme in this blog post shows it clearly.īack to our example. For example, I was able to fetch the value of productId if the Page URL was But in today’s blog post, let’s imagine that we have a URL of the following structure:, and we want to turn the category_name into a GTM variable (knowing that the category name is always in the 2nd subdirectory of the Page Path. ![]() In that blog post, I was working with query parameters. This blog post might sound similar to the one that I’ve recently published, How to extract a query parameter of the URL, but this time a bit different issue is solved. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |