Embed/inserting additional tracking code

I need to be able to embed links with tracking code in the copy for my albums. I use Google Analytics. I can get the links in there using LR Publisher, but it doesn’t seem to like the “tracking code” part of it. Is there a way to embed this sort of tracking code in:

LR Publisher > Edit Album > Page Content > Main Copy? I’ve been able to insert a normal link there, but not one that looks like this:

<a href="http://www.example.com" onclick="captureOutboundLink('http://www.example.com'); return false;">Check out example.com</a>

I have to come up with some sort of solution for tracking links to external sites. Thank you for any assistance.

pj

What isn’t working? Is the link not appearing on the page? Or is it not performing as expected?

I just tried using your code in a Lightroom published album by adding it to the copy area of the album in Lightroom. The link appeared in the album with all the code intact.

can you post a link to an album where it’s not working as expected?

Hi Rod

Thanks for the assist.

After I insert the code in LR Publisher > Edit Album > Page Content > Main Copy, and I “publish” the revision, I get this message:

“Warning. Can’t rename the published collection. Error performing function. Code: 403)”

When I go back to “LR Publisher > Edit Album > Page Content > Main Copy”, the changes I made are gone; were not saved.

I think there may be a bigger problem here because the Google code required to process the call is not in/on the page which means I would have to find a way to insert that code chunk into the header. If I can’t do this, then this topic is moot other than I have a monetary interest in being able to log these clicks.

Do I make template pages for each of the galleries? (PIA but doable)

And even if that’s a solution, how do I insert the Google code in the header?

  <script> ;
      var captureOutboundLink = function(url) {
      ga('send', 'event', 'outbound', 'click', url, {
         'transport': 'beacon',
         'hitCallback': function(){document.location = url;}
      });
    }
  </script>

One solution I keep coming back to may be Google Tag Manager. Is it possible the required Tag Manager snippets could be included for every page? Here’s a reference:

If anyone else here is pondering these same issues, please chime in if you have better ideas.

Thanks!

That error seems more like a Publisher error.
You can always add text to an album’s copy area from Backlight Publisher. It’s easier to write in than Lightroom Publisher.

To insert the script you’d need to use phplugins. You could use either the scripts hook or the head hook (if the code definitely needs to go in the head).
Be sure to comment out those single quotes in the code, or change them to double quotes. (single quotes will end the echo statement you need to use to add the code).
(and don’t have a semi colon after the first script tag)

Rod, just use double quotes with the echo statement instead:

function head(){
   echo "
    <script> ;
         var captureOutboundLink = function(url) {
            ga('send', 'event', 'outbound', 'click', url, {
               'transport': 'beacon',
               'hitCallback': function(){document.location = url;}
            });
       }
     </script>
  ";
}

ah, much simpler.

1 Like

Thanks, Guys!!

I need a few days to get to this, but I will give this a try and give you an update.

pj

Strange but I found you have to insert:

onclick="captureOutboundLink('http://www.example.com'); return false;">

Immediately after <a.

It doesn’t matter whether I use Publisher or the LR Plugin. Other than needing to remember this is how it works, the quirk doesn’t bother me. The code works and that’s all that matters to me.

Thanks for the code, Daniel! Worked as advertised.

Many thanks for the assist, Rod!

1 Like