0

I have a web page that the title is changed from 'Pagename' to '(1) Pagename' when there is an update on the page. That number increments to 50 each time there is a new update and then is maxed out showing '(50+) Timeline'.

When logging page views, Google Analytics shows the '(n) Pagename', which I don't want. So I found out how to manually change to logged page title, _gaq.push(["_set", "title", 'new title']);.

So my question is, how do I most efficiently remove the (1-50)/(50+) prefix and just get 'Pagename'? Is regex best for this?

This is what I'm using based on the answer from Ross:

var window_title = window.title.replace(/^\(\d+\+?\)\s/, '');
_gaq.push(["_set", "title", window_title]);

1 Answer 1

2

Yes, RegEx can do that.

window.title.replace(/^\(\d+\+?\)\s/, '');

Of course it depends on what software your site is using as perhaps it would be possible to just output the page title without that prefix in the relevant part of the template. So echoing that directly into the Google Analytics tag. But I think the above javascript is probably the easier solution to implement.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd like to check if the prefix exists, then if it does, manually set it without the prefix. So something like if(prefix_exists_in_title){ var new_title = window.title.replace(/^\(\d+\+?\)\s/, ''); _gaq.push(["_set", "title", new_title]); } How would I get the True/False for the if statement?
@Justin - there's really no point in doing the test first; you'd be doing a match followed by a match+replace for the True case which is more expensive than just blindly applying the regex. It would be worth it if you had a large block of code to execute when matched. That said, you can do mystring.match(myregex) which returns an array of the match(es), or null on no match.
Well said. Thanks for your help.

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.