2009
04.01

I use Lightroom’s built-in web template for maintaining my portfolio on automagisk.no. The management of the portfolio can be one entirely from within Lightroom, which is very nice. Updating the website can be done with a button: Lightroom automatically transfers and updates both images, HTML pages and CSS.

There are lots of image gallery software available, but I want my portfolio to be very easy to maintain, and I do not want to re-type the captions, etc that I already have in Lightroom. Also: I want the portfolio to be in HTML so it can be searched and bookmarked, not in Flash, Silverlight or something similar.

The only thing I am missing from Lightroom’s Web module is the option to add Google Analytics code to the web pages. I use Google Analytics to logs all traffic data on my blog (the one you are reading now), and I want to log traffic from the portfolio in the same way.

Although Lightroom does not allow you to enter the Google Analytics code, you can edit the HTML templates that Lightroom uses for generating the web pages. It does require a little hacking, but it is not difficult.

Google Analytics visitor map
One of Google Analytics’ many features: Visitor map.

To do this, you will need:

Warning

There is one small, but annoying side-effect of this: Every time you go to the Web module in Lightroom, it will open up the Google Analytics JavaScript file http://www.google-analytics.com/ga.js in a web browser. I do not know why, or how to avoid it. It doesn’t do any harm, so I think it is worth it.

Also: You are responsible for everything you do, so even though I have done this and it worked perfectly for me, do not blame me if it for some reason doesn’t work for you.

Editing the Lightroom web templates

Here are the step-by-step instructions on how to add Google Analytics JavaScript to the default HTML web template on Apple Mac OS X (if you know how to do this on Microsoft Windows, please use the comment form below):

  1. Start the Terminal app. It opens up a UNIX command window.
  2. Change to the directory of the default HTML template (this can be done in one step, but the line would be very long, so I split it into three commands): cd "/Applications/Adobe Lightroom 2.app/Contents/PlugIns/" cd Web.lrmodule/Contents/Resources/cd galleries/default_html.lrwebengineIt is not posible to change to this directory using Finder, because it hides all the contents of the “Lightroom 2. app” (it is a directory, not a file).
  3. The Google Analytics JavaScript code will be placed in the file foot.html, which is being used when creating all the HTML files. Just to be sure: Make a backup of that file: cp foot.html foot.original
  4. Now it’s time to edit “foot.html” and add the Google Analytics JavaScript code. You can use any text editor for this, but some will not allow you to open files “inside” the “Lightroom 2.app” directory, as it regards “Lightroom.app” as a file. I prefer the emacs editor (automatically installed with Mac OS X, emacs cheat sheet here), which is started with the command emacs foot.html
  5. Scroll to the end of the file, and add your Google Analytics code right above the line with “</body>” (you must replace “UAXXXXXXX-X” with your Google Analytics id, of course):
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview();
    } catch(err) {}</script>
  6. Save the file: Ctrl-x Ctrl-s
  7. Exit emacs: Ctrl-x Ctrl-c
  8. Start Lightroom
  9. Select some images
  10. Go to the Web module and create a web gallery using the “HTML Gallery (default)” template from the Template Browser on the left-hand side. This makes sure that we are using the HTML templates that have just been updated.
  11. Change colors and other web template settings as you want to.
  12. Click the “Export…” button to save the generated HTML pages somewhere on your drive.
  13. Open the newly generated “index.html” file in your web browser and check that the page looks as it should.
  14. View the source, and verify that the Google Analytics code is present near the end of the HTML source.
  15. Upload all the generated files to your web server.
  16. Pat yourself on the back!

You can log in to Google Analytics right away, but it will take a while (a day, maybe?) before any traffic data will be available.

Was this useful? Please let me know in the comments – thanks!

Possibly related posts

  1. Backup of images, Lightroom catalog and other things using RAID, rsync, etc When image archives consisted of albums with negatives or cassettes with slides, having a good archiving system was crucial, otherwise it would be very difficult to find anything. With digital...
  2. Adobe Photoshop Lightroom: Workflow and tips I use Adobe Photoshop Lightroom for almost everything I do with my images: Import files from CF cards File renaming and directory building Adding metadata and default settings to...
  3. Lightroom develop preset for Nikon D300 Adobe provides some good camera calibration presets for Lightroom (included in Lightroom 2.3), but you may want to tweak them some more. I have spent a lot of time tuning...
  4. Time-lapse with D300, Lightroom and Final Cut Express Time-lapse of about 1500 RAW images taken between 22:00 and 01:00 on E6 between Skedsmokorset and Kløfta on a freezing November night. Tommy Johansen in Veidekke contacted me last...

15 comments so far

Add Your Comment
  1. This is a GREAT little trick … LOVE it!

    |Brandon Oelling
    {x=}

  2. Do you know how to conditionalise this with a checkbox?
    using checkbox_row you can leave it off until just before you export and avoid having ga.js open each time.

    In code:
    model section

    ["nonCSS.useAnalytics"]=false,

    views section

    f:checkbox_row {
    title = “Insert Analytics Code”,
    bindingValue = “nonCSS.useAnalytics”,
    },

    in html
    Google code

  3. Ouch.. the conditional code got eaten..

    Okay try again

    HTML
    <% if model.nonCSS.useAnalytics then %> $model.nonCSS.Analytics <% else %><% end %>

  4. Lightroom Blog: Thanks, but which file(s) should this be in? I am just modifying the default Lightroom HTML template, and I don’t know any Lua..

  5. This is VERY straightforward on a PC, simply explore yourself to C:\Program Files\Adobe\Adobe Photoshop Lightroom 2\shared\webengines\default_html.lrwebengine and edit foot.html. Thanks for the tip!!

  6. This is brilliant but being a Terminal novice I struggled to even switch to the Lightroom directory until i read an article by Chris Stone which suggested

    “cool Terminal trick that can save some time is to just drag the actual folder icon of the directory in question from the Finder desktop right into the Terminal window (after you’ve typed cd and space, for example), and the fully escaped pathname will get entered right into the command line for you.”

  7. Didn’t know it worked that way – thanks for the tip!

  8. hey all. you actually don’t need to use terminal. you can use finder for everything.

    using the he provided above, you’ll get to items that when clicked on in finder look like dead ends. for the items like this (such as resources, plugins, and default_html.lrwebenginedefault_html.lrwebengine) in finder, just control click on them and then click on “show package contents.” a new window will open and you can keep moving : D

    after making the changes to foot.html, when trying to save finder will probably ask you for your admin password to save the foot.html file.

  9. using the *paths* he provided above : D

  10. I’ve wanted to do this forever, but couldn’t find the template. Thanks.

  11. I already used this in LR 1 on my PC. But didn’t know how to find it on my new Mac. The code in LR 1 also gives a error window in Windows, but not anymore in LR 2! Too bad LR 2 on the mac opens the js code…

    I’m very curious how Lightroom Blog added the code. Guess the first code he/she is talking about, has to be placed into the galleryInfo.lrweb file. (model and views section). The other code into foot.html? But how, still have to find out. I’m a very noob at scripting :P

  12. Thanks a lot. Your post really helped in figuring out how to add Facebook Comments to a Lightroom web template. If you’re interested in seeing how to add Facebook Comments you can check out my post:
    http://so.ca/?p=265

    • Cool! I might add Facebook comments myself – thanks :-)

  13. Hi, you can add some code to disable the script for preview with

    your google script

    • sorry the code doen’t appear in my previous comment

      <% if mode ~= ‘preview’ then %>
      your google script
      <% end %>