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 7976825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:00:51+00:00 2026-06-04T09:00:51+00:00

This has been bugging me for a few days now and I have not

  • 0

This has been bugging me for a few days now and I have not found a way to get it to work. My site is using Jquery, Jquery-Mobile and RequireJS and I want to add Google Analytics to the mix.

My Analytics setup is like so (see also here):

<script type="text/javascript">
    var _gaq = _gaq || [];
    (function() {
         ... regular stufff 
    })();
// bind to pageshow event
$(document).on('pageshow', 'div:jqmData(role="page").basePage', function (event, ui) {
try {
    _gaq.push(['_setAccount', 'ID';
    _gaq.push(['_gat._anonymizeIp']);
    var url = location.href;
        try  {
            if (location.hash) {
                url = location.hash;
                }
            _gaq.push(['_trackPageview', url]);
            } catch(err) {}
        }); 
    </script>

So I’m listening for the JQM pageshow event to track for Analytics. This snippet is pasted into the end of all pages.

I have fiddled Analytics into requireJS like this:

Main.js:

require.config({ 
    baseUrl: "../js/",  
    paths: {
        "jquery":        "libs/jquery/jquery.min",
        "jqm":           "libs/jquery-mobile/jquery-mobile.min", 
        "analytics":     "https://www.google-analytics.com/ga"
        }

require(['order!jquery', 'order!jqm', 'order!analytics', 'app'],
    function(jquery, jqm, analytics, App){  
        App.start();        
        });

And in App.js:

var start = function() {
   require(['order!jquery', 'order!jqm', 'order!analytics'],function() {
    // do stuff
   });

Problem is, the Analytics snippet is at the end of every page HTML markup, so although I think I have set it up correctly in requireJS with Jquery and Jquery Mobile loading before, I’m still getting an error:

 $ is not defined
  ....ocument).on('pageshow', 'div:jqmData(role="page").basePage', function (event, ui...

when the first page loads.

I guess because HTML markup is outside the reach of require and is parsed before JQM is loaded. Still, there has to be a way to set this up properly.

Question:
Any idea why the error pops up and what I can do about it?

Thanks for help!

EDIT:
Ok. Doing it like this works:

Inside my page header I’m declaring the first part:

<script type="text/javascript">
var _gaq = _gaq || [];

(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>

Then inside my requirejs start function I’m calling the 2nd snippet:

    var start = function() {
        require(['order!jquery', 'order!jqm'],function() {
            var analyticsHasBeenSet; 
            ...
            var analyize = function () {

                if ( analyticsHasBeenSet != true ){
                    analyticsHasBeenSet = true;
                    require(['https://www.google-analytics.com/ga.js'], function (App) {

                        $(document).on('pageshow', 'div:jqmData(role="page"), function() {
                            _gaq.push(['_setAccount', '']);
                            _gaq.push(['_gat._anonymizeIp']);
                            var url = location.href;
                            try  {
                                if (location.hash) {
                                    url = location.hash;
                                    }
                                _gaq.push(['_trackPageview', url]);
                                } catch(err) { // error catch }
                            });
                        });
                     }
                 }
            analyize();

This way the 2nd snippet will only be executed once Jquery and Jquery Mobile have been loaded. By setting a global variable (I hope) the binding will only be initiated once. So far seems to work.

  • 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-06-04T09:00:54+00:00Added an answer on June 4, 2026 at 9:00 am

    The problem is most likely around your use of the order! plugin. This should only be used with standard .js files, and not modules.

    The order plugin is best used with traditional scripts. It is not
    needed for scripts that use define() to define modules. It is possible
    to mix and match “order!” dependencies with regular dependencies, but
    only the “order!” ones will be evaluated in relative order to each
    other.

    http://requirejs.org/docs/api.html#order

    Your analytics path should be modified to be a traditional js file (as ga.js does not register itself as a module)

    "analytics":     "https://www.google-analytics.com/ga.js"
    

    jQuery and jQuery mobile do register themselves as modules, so your config for those is correct.

    In your require call, all you need is:

    require(['jquery', 'jqm', 'analytics'],function($, jqm) {
        // analytics is not a module so _gaq will now exist as a global var.
       });
    

    Seeing as the ga.js is the only file that isn’t a module, the order! plugin will probably be of little use as it only matters when you have multiple traditional scripts that need to load in a certain order.

    The order plugin is best used with traditional scripts. It is not
    needed for scripts that use define() to define modules. It is possible
    to mix and match “order!” dependencies with regular dependencies, but
    only the “order!” ones will be evaluated in relative order to each
    other.

    However, it is worth noting that the code snippet provided by Google for using Google Analytics already accounts for async loading so it might be unnecessary to use Require at all.

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

Sidebar

Related Questions

This has been bugging me for a few days now. I'm trying to migrate
This has been bugging me for the last few hours now. I am using
This has been bugging me for 2 days now and I cant get my
This has been bugging me for more than two days now, so i thought
This has been bugging me for a couple days now. A list has content
This one has been bugging me for a while now. Is there a way
This has been bugging me for days, cant seem to get an answer after
It's not a programming questions. But this question has been bugging me for awhile.
This Jquery problem has been bugging me for a while now. I developed a
This has been driving me crazy for the past few minutes I have a

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.