Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Blog Module /

Discuss the Blog Module.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

TinyMCE Strips onclick and other items


Go to End


6 Posts   3832 Views

Avatar
Hello_electro

Community Member, 80 Posts

10 August 2011 at 2:51am

Hi All:

I need to use Google Analytics to track outbound links in my blog posts. I am having a really hard time trying to figure out the right way to get TinyMce to stop removing code I am adding via the html option.

I found a couple related topics in the forum, but nothing seemed to pan out when I tried and the posts were related to rather old versions.

Has anyone had to use the onclick in their links for google outbound tracking? Any insight would be greatly appreciated!

thank you!

Avatar
MarcusDalgren

Community Member, 288 Posts

10 August 2011 at 4:55am

If you need to add that kind of stuff then TinyMCE really isn't the best tool for the job. Is there no way you can add the click handlers from an external script automatically instead of trying to add them via TinyMCE?

Avatar
Hello_electro

Community Member, 80 Posts

10 August 2011 at 6:01am

I have heard that mentioned before. Any references I can look at on how to do this? Not proficient at js.

Thanks!

Avatar
MarcusDalgren

Community Member, 288 Posts

10 August 2011 at 6:29am

I've never done this for google. Is the code you need to add always the same or does it differ?
If you want to ease your way into js I recommend looking at jquery.

Avatar
Hello_electro

Community Member, 80 Posts

10 August 2011 at 7:31am

 <a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;"> </code]

for the most part it is the same. The example.com i believe can be anything you want. i believe that is what is reported back to google to identify the link to you in analytics.

I read something about using the rel attribute and applying that in the tinymce and having jquery add in the above onclick code. Again, not totally understanding of jquery/javascript makes me need to do some more research!!

Avatar
MarcusDalgren

Community Member, 288 Posts

10 August 2011 at 8:00am

Ok if you get jquery you can do this:
First of all, put this code in your main Page.ss file before the closing body tag.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
  try {
    var pageTracker=_gat._getTracker("UA-XXXXX-X");
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}
$("#YourContentArea a").click(function(event) {
  event.preventDefault();
  recordOutboundLink(this, 'Outbound Links', this.href);
});
</script>

Make sure you put in your own analytics code in there. Also #YourContentArea needs to be replaced with either the id or the class of the place where the content from the TinyMCE editor ends up on your page.