2

I need to simplify url strings for some reporting, and sometimes our url strings have gclid params baked into them at different parts:

https://www.website.com/colors/blue?src=camp_id1&gclid=Cj0KCQjwoInnBRDDARIsANBVyASfVUCg4ShKaAq4q8cX6xgB4kYj-oRGEPuO5UuUaAnZi2zD7yQU684aAo3KEALw_wcB

or

https://www.website.com/colors/blue?gclid=Cj0KCQjwoInnBRDDARIsANBVyASfVUCg4ShKaAq4q8cX6xgB4kYj-oRGEPuO5UuUaAnZi2zD7yQU684aAo3KEALw_wcB&src=camp_id1

or

https://www.website.com/colors/blue?page=2&gclid=Cj0KCQjwoInnBRDDARIsANBVyASfVUCg4ShKaAq4q8cX6xgB4kYj-oRGEPuO5UuUaAnZi2zD7yQU684aAo3KEALw_wcB&src=camp_id1

or

https://www.website.com/colors/blue?page=2&src=camp_id1

So there are all these variations of the URL. Sometimes they contain the gclid and sometimes they don't. They're often at different parts in the url.

What's the easiest way in ruby to print the string without the gclid substring if it's there?

3 Answers 3

3
.sub(/[\?&]gclid=[^&]*/, '')
Sign up to request clarification or add additional context in comments.

2 Comments

It's not necessary to escape a question mark within a character class.
Keep in mind that, from an educational point of view, you are instructing readers (who don't read comments :-)) that a question mark within a character class must be escaped.
1

If all you need is the base URL you can do:

url = your_url_string
trimmed_url = url[0, url.index("?")]

Or you can guard against strings without query params:

url = your_url_string
trimmed_url = url[0, (url.include?('?') ? url.index("?") : url)]

There may be a way to do it with URI as well...

If all you need to do is remove that parameter, see this answer: Rails String Replace URL Parameters

1 Comment

I need to keep the other params, but just get rid of the gclid.
1

I would recommend using the URI library for this instead so you can just parse the query portion:

uris = ["https://www.website.com/colors/blue?src=camp_id1&gclid=Cj0KCQjwoInnBRDDARIsANBVyASfVUCg4ShKaAq4q8cX6xgB4kYj-oRGEPuO5UuUaAnZi2zD7yQU684aAo3KEALw_wcB",
"https://www.website.com/colors/blue?gclid=Cj0KCQjwoInnBRDDARIsANBVyASfVUCg4ShKaAq4q8cX6xgB4kYj-oRGEPuO5UuUaAnZi2zD7yQU684aAo3KEALw_wcB&src=camp_id1",
"https://www.website.com/colors/blue?page=2&gclid=Cj0KCQjwoInnBRDDARIsANBVyASfVUCg4ShKaAq4q8cX6xgB4kYj-oRGEPuO5UuUaAnZi2zD7yQU684aAo3KEALw_wcB&src=camp_id1",
"https://www.website.com/colors/blue?page=2&src=camp_id1"
]

uris.map do |s| 
  uri = URI.parse(s)
  query_params = uri.query.scan(/\w+(?==)[^&]*/).reject{|s| s.start_with?('gclid')}
  uri.query = query_params.empty? ? nil : query_params.join('&')
  uri.to_s
end

# => ["https://www.website.com/colors/blue?src=camp_id1", 
#     "https://www.website.com/colors/blue?src=camp_id1", 
#     "https://www.website.com/colors/blue?page=2&src=camp_id1",  
#     "https://www.website.com/colors/blue?page=2&src=camp_id1"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.