Skip to content

Add tracking tokens to WordPress

Just had to do a search and replace to add in tracking tokens site-wide to a wordpress site.  I tried some search & replace plugins, but I found them all a bit buggy, so I opened up PHP My Admin to handle it. I had a list of links so I just ran them all like:

UPDATE wp_posts SET post_content = REPLACE (post_content,'','');

then ran the following, because I knew a few links already had the tracking token added:

UPDATE wp_posts SET post_content = REPLACE (post_content,'?UTM=token?UTM=token','?UTM=token');

And, last but not least I verified that all links where updated by running:

SELECT post_content FROM wp_posts WHERE (CONVERT(`post_content` USING utf8) LIKE '%%' AND CONVERT(`post_content` USING utf8) NOT LIKE '%%UTM%%')

So, I’m basically just checking that every post with that domain also has the token code in it somewhere.

You can be a bit more thorough by using regex, but that should do the trick if you only have a few links to update.

UPDATE 6/2016

I just needed to do a similar task, but I needed to add a tracking token to all links to a domain, so I needed a bit of regex.  This time I found a nice search and replace plugin that supports regex. And I used these commands to first add in the token:

Search pattern: ((<a href="(http://)?(www.)?*?))")
Replace pattern: $1?UTM=token

This will pick up all links, weather they have http or not, and weather they have www. or not.  And again I ran this kind of replace to fix any links that already had tracking added to them:

Search pattern: ?UTM=token?UTM=token
Replace pattern: ?UTM=token
Published inScripting

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *