Search code examples
backbone.jsgoogle-analyticsr.js

Google Analytics optimization with r.js


Hi everyone I am trying to make r.js work but i got error on Google Analytics like this

Tracing dependencies for: main
Cannot optimize network URL, skipping: //www.google-analytics.com/analytics.js

this is my requierjs.config file

window.GoogleAnalyticsObject = "__ga__";
window.__ga__ = function() {
    for (var i=0; i<arguments.length; i++) {
        var arg = arguments[i];
        if (arg.constructor == Object && arg.hitCallback) {
            arg.hitCallback();
        }
    }
};
window.__ga__.q = [["create", "UA-82626142-1", "auto"]];
window.__ga__.l = Date.now();

paths: {

    ga: "//www.google-analytics.com/analytics"
},
shim: {
    "ga": {
        exports: "__ga__"
    },
}

and this is my r.js config file

{
    baseUrl: "../js",
    name: "main",
    out: 'app-built.js',
    findNestedDependencies: true,

    paths: {
        ga: "//www.google-analytics.com/analytics",
    },
    include: 'requireLib',
    mainConfigFile: "../js/main.js",

}

I have used ''empty path for r.js config but i dosn`t work. Thanks in advance


Solution

  • I have checked Requierjs documantation again and again and i found the right way to make cdns(urls) optimize with r.js. Need write in optimize config file

    paths: {
        ....
        ga:'empty'
    }
    

    we can write 'empty' for any cdn link just need use console command when we run optimize config file like this (this is my own example from my app)

    'node build/r.js -o build/build.single.js paths.ga=empty:' 
    

    Hope this will be usfulle for others who have same probleme