Baidu statistics and Google statistics of international products

Recently, I am leading the reconstruction of an international product of the company, and it is necessary to carry out buried point statistics. The specific requirement is that the event analysis in the project is based on domestic/foreign statistics, the overseas version uses Google statistics, and the domestic version uses Baidu statistics. The same set of engineering code is used, so only one method needs to be written, which distinguishes whether it is Baidu statistics or Google statistics according to the platform properties of the configuration file. The code is summarized as follows:

;(function (win) {
    var config = win.SYSTEM_CONFIG;
    var statistics = function () {

    };
    if (config && (config.baidu || config.google)) {
        var platform = config.platform;
        if (platform ==='domestic') {
            var _hmt = _hmt || ,
            (function () {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?" + config.baidu;//Baidu statistics key
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();
            statistics = function (category, action, opt_label, opt_value) {
                //_hmt.push(['_trackEvent', category, action, opt_label, opt_value]);
                _hmt.push(['_trackEvent', category, action, opt_label || "", opt_value || 1]);
            }

        } else if (platform === '海外') {
            /*- Global site tag (gtag.js) - Google Analytics --*/
            document.write('<script async src = "https://www.googletagmanager.com/gtag/js?id=' + config.google + '" > <' + '/script>');//谷歌统计的key
            window.dataLayer = window.dataLayer || [];

            function gtag() {
                dataLayer.push (arguments);
            }

            gtag('js', new Date());
            gtag('config', config.google);
            statistics = function (category, action, opt_label, opt_value) {
                gtag('event', action, {
                    'event_category': category,
                    'event_label': opt_label || "",
                    value: opt_value || 1
                });
            }
        }
    }
    window.Statistics = statistics;
})(window);

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324887751&siteId=291194637