How do I add/replace Google Analytics code using regular expression?

Posted on 2011-05-01
Last Modified: 2013-12-25
Google Analytics code contains special symbol such as quotation and semicolon marks.

I'm using Perl to dynamically update and replace certain content of my html template to generate static page.  For specific purpose, I can't simply just add the tracking code to the html page.

I would like to use regular expression to match for </head>

and then append the Google Analytics code before the closing </head>

How can this be done?
Question by:jay28lee
    LVL 40

    Accepted Solution

    Assume var $google has the analytics

    Try a replace:

    $html =~ s/</head>/$google<\/head>/s;


    Author Comment

    how do i assign a piece of code to a variable with special symbol of quotation and semicolon marks?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now