ThemeShaper Forums » Thematic

[closed]

Adding Google Analytics code in footer

(9 posts)
  • Started 9 years ago by zanguanga
  • Latest reply from dandam
  • This topic is resolved
  1. Hello
    I do not know what I do bad in childtheme, but does not work

    function childtheme_after() {
    return '
    <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-0000000-0");
    pageTracker._trackPageview();
    } catch(err) {}</script>
    ';
    }
    add_filter('thematic_after', 'childtheme_after');

    Posted 9 years ago #
  2. Hi,

    this can't work .. you're trying to filter an action hook :-)

    Ok .. it's:

    function childtheme_after() {
    echo '
    <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-0000000-0");
    pageTracker._trackPageview();
    } catch(err) {}</script>
    ';
    }
    add_action('thematic_after', 'childtheme_after');

    Chris

    Posted 9 years ago #
  3. Hi,
    you have already verified that I am not programmer :/

    This gives back an error to me in line 42
    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'

    Line 42:

    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

    Posted 9 years ago #
  4. Sorry .. checked only the method not the code.

    Chris

    Posted 9 years ago #
  5. Finally I have put the code in footer.php :/

    Posted 9 years ago #
  6. cotinus
    Member

    @zanguanga

    Glad it's working!

    Can you share what you changed in the code?

    Posted 9 years ago #
  7. function childtheme_after() {
    echo '<script type="text/javascript">';
    echo 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
    echo 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
    echo '</script>';
    echo '<script type="text/javascript">';
    echo 'try {';
    echo 'var pageTracker = _gat._getTracker("UA-0000000-0");';
    echo 'pageTracker._trackPageview();';
    echo '} catch(err) {}</script>';
    }
    add_action('thematic_after', 'childtheme_after');

    You need to escape the '

    Chris

    Posted 9 years ago #
  8. @cotinus
    I have copied the file footer.php into the folder of childtheme, and have written tehe Google Analytics code right before the label </body>

    @Chris
    Vielen Dank! your code works perfectly :-)

    Posted 9 years ago #
  9. Thanks for the escape tip/reminder.

    Using Thematic 0.9.5.1 I was able to use the thematic_after action to add GA script to the footer no problem.

    Posted 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.