Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:58:56+00:00 2026-05-26T16:58:56+00:00

I’m retrieving content via a jQuery get Ajax call and add it to an

  • 0

I’m retrieving content via a jQuery get Ajax call and add it to an element afterwards. The HTML code is added without any problem, just the JS code gets lost. What am I doing wrong? When I look at the response in Firebug it is still there, it’s just not added to the HTML element.

My Ajax request:

function get_overlay_content(path)
{
    $.get(path, function(data) { 
        $("#overlay_content").html(data).fadeIn(500); 
        $(function()
     {
        // do something 
     });
    });
}

The response in Firebug:

<div id="imprint" class="overlay_box">
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXX-1']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
    <div id="overlay_close" onclick="close_overlay();"><img src="/2012/images/overlay_close.png" id=""></div>
# ETC

The HTML after update:

<div id="overlay_content">

<div id="imprint" class="overlay_box">

    <div id="overlay_close" onclick="close_overlay();"><img src="/2012/images/overlay_close.png" id=""></div>
    <div class="overlay_heading"><img src="/2012/images/heading_download.png" height="65"></div>

# etc.

My guess is that it has something to do with encoding. What do you think?

Thanks!
Olaf


SOLVED:

Thanks to Uzi, the solution was quite simple, following the Google guide Asynchronous Tracking Usage Guide

But for everyone to understand: If you have one main file (index.html) from where you retrieve partials (lets call it /partials/_download_pdfs.html) via an AJAX GET request (e.g. jQuery.get()) all you have to do to track every AJAX request with Google Analytics is the following:

Add the tracking code for Analytics ABOVE the rest of your Javascript
and your AJAX GET request, put the following code in a callback function (if you don’t put it in a callback it might slow down your actual request)

$.get(path_to_track, function(data) { 
  // Fade in effect:
  $("#container").html(data).fadeIn(500);

  // this part is interesting:
  _gaq.push(['_setAccount', 'UA-XXX-1']);
  _gaq.push(['_trackPageview', path_to_track]);

  // Do other stuff in the callback
});

That’s it. If you want to track PDF downloads on a page you retrieved with an AJAX GET request with jQuery (as I did), just add the following code to the partial (eg /partials/_download_pdfs.html)

<script type="text/javascript">

        $(document).ready(function() {

            $(".pdf_link").click(function(){
                _gaq.push(['_setAccount', 'UA-XXX-1']);
                _gaq.push(['_trackPageview', '/pdf/' + $(this).attr("id")]);
            });
        });
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:58:57+00:00Added an answer on May 26, 2026 at 4:58 pm

    If I understand correctly, you are fetching HTML code using AJAX.
    That block of code contains a script tag, and you expect the script to run one the request is complete.
    Unfortunately it doesn’t work this way, since as far as the DOM concerned, this is just an element.

    I’m not sure why you need an AJAX code for google analytics to run, but in case it contains dynamic data, just make the AJAX call return the parameter you need and have a function running this code locally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.