// +------------------------------------------------------------+ // | PUBLIC FUNCTIONS | // +------------------------------------------------------------+ function ct_trackArticleView(id, sdid) { var t = new _t_ct_pt(id, sdid, 2); t.track_page_int('if/a'); } function ct_trackBlogView(id, sdid) { var t = new _t_ct_pt(id, sdid, 5); t.track_page_int('if/b'); } function ct_trackPageView(id, sdid) { var t = new _t_ct_pt(id, sdid, 1); t.track_page_view(); } function ct_trackPhotoView(id, sdid) { var t = new _t_ct_pt(id, sdid, 3); t.track_page_view(); } function ct_trackRadioListen(id, sdid) { var t = new _t_ct_pt(id, sdid, 6); t.track_page_int('if/r'); } function ct_trackNewsView(id, sdid) { var t = new _t_ct_pt(id, sdid, 7); t.track_page_int('if/n'); } function ct_trackBrowsedPage(id, sdid) { var t = new _t_ct_pt(id, sdid, 8); t.track_page_view(); } function ct_trackSongPlay(id, sdid, tpid) { var t = new _t_ct_pt(id, sdid, 4); t.set_tpid(tpid); t.track_page_view(); } function ct_trackSongDownload(id, sdid, tpid) { var t = new _t_ct_pt(id, sdid, 10); t.set_tpid(tpid); t.track_page_view(); } function ct_trackRingtoneDownload(id, sdid, tpid) { var t = new _t_ct_pt(id, sdid, 11); t.set_tpid(tpid); t.track_page_view(); } function ct_trackJcpSmsc(id, sdid, tpid) { var t = new _t_ct_pt(id, sdid, 10000); t.set_tpid(tpid); t.track_page_view(); } // +------------------------------------------------------------+ // | CLASSES | // +------------------------------------------------------------+ function _t_ct_pt(id, sdid, tid) { this.version = 'v1'; this.id = id; this.sdid = sdid; this.tid = tid; this.key = null; this.referer = encodeURIComponent(window.location.href); this.ctutn = null; this.ctutv = null; this.tpid = null; this.rcs_l = null; this.rcs_t = null; this.rcs_d = null; this.rcs_i = null; // +----------------+ // | Public Methods | // +----------------+ this.get_page_view_tracker = function() { return this._get_tracking_base_url() + 't.gif' + '?c=' + this.id + '&r=' + this.referer + '&s=1' + '&t=' + this.tid + this._get_key_qs() + this._get_ctu_qs() + this._get_tpid_qs() + this._get_rcs_qs(); }; this.send_request_get = function(url, callback) { var req = this._create_xml_http_request(); req.onreadystatechange = function() { if (req.readyState == 4) { if (window.eval(callback)) { eval(callback + '(' + req.responseText + ')'); } } }; req.open('GET', this._cache_bust(url), true); req.send(''); return false; }; this.set_rcs = function(l, t, d, i) { this.rcs_l = encodeURIComponent(l); this.rcs_i = encodeURIComponent(i); this.rcs_t = t; if (t.length > 100) { this.rcs_t = t.substr(0, 97) + '...'; } this.rcs_d = d; if (d.length > 250) { this.rcs_d = d.substr(0, 247) + '...'; } this.rcs_t = encodeURIComponent(this.rcs_t); this.rcs_d = encodeURIComponent(this.rcs_d); }; this.set_tpid = function(tpid) { this.tpid = encodeURIComponent(tpid); }; this.track_page_int = function(ep) { var iframe; iframe = document.createElement('iframe'); iframe.scrolling = 'no'; iframe.setAttribute('allowTransparency', 'true'); iframe.setAttribute('frameborder', '0'); iframe.setAttribute('style', 'border:none; overflow:hidden; width:0px; ' + 'height:0px; visibility:hidden; ' + 'display:none;'); iframe.src = this._cache_bust(this._get_base_url() + ep + '.html?c=' + this.id + '&r=' + this.referer + '&sdid=' + this.sdid + '&tid=' + this.tid + this._get_tpid_qs() + this._get_rcs_qs()); document.body.appendChild(iframe); }; this.track_page_view = function () { var img; img = document.createElement('img'); img.id = 'tpv'; img.width = '1'; img.height = '1'; img.src = this._cache_bust(this.get_page_view_tracker()); img.alt = ''; document.body.appendChild(img); }; // +-----------------+ // | Private Methods | // +-----------------+ this._cache_bust = function(url) { return this._url_adjoin_qs(url, { api_z : Math.random() }); }; this._cast_to_string = function(a) { if (typeof a == 'boolean') { return a ? '1' : '0'; } if (typeof a == 'object' && !a) { return ''; } return encodeURIComponent('' + a); }; this._create_xml_http_request = function() { try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { return new XMLHttpRequest(); } }; this._get_base_url = function() { return document.location.protocol + '//' + this._get_hostname_from_url(this.version) + '/' + this.version + '/'; }; this._get_ctu_qs = function() { var qs; qs = ''; if (this.ctutn != null && this.ctutv != null) { qs = '&' + this.ctutn + '=' + this.ctutv; } return qs; }; this._get_env_prefix = function() { switch (this.sdid) { case 1: return 'local-'; case 2: return 'qa-'; default: return ''; } }; this._get_hostname_from_url = function(version) { var scripts = document.getElementsByTagName('script'); var re_match = new RegExp('/' + version + '/a.js'); var re_ext = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im'); for (i = 0; i < scripts.length; i++) { if (scripts[ i ].src.match(re_match)) { return scripts[ i ].src.match(re_ext)[ 1 ].toString(); } } }; this._get_key_qs = function() { var qs; qs = ''; if (this.key != null) { qs = '&k=' + this.key; } return qs; }; this._get_rcs_qs = function() { var qs; qs = ''; if (this.rcs_l != null && this.rcs_t != null && this.rcs_d != null) { qs = '&rcs_l=' + this.rcs_l + '&rcs_t=' + this.rcs_t + '&rcs_d=' + this.rcs_d + '&rcs_i=' + this.rcs_i; } return qs; }; this._get_tpid_qs = function() { var qs; qs = ''; if (this.tpid != null) { qs = '&tpid=' + this.tpid; } return qs; }; this._get_tracking_base_url = function() { return document.location.protocol + '//' + this._get_env_prefix() + 't.crowdtwist.com/' + this.version + '/'; }; this._url_adjoin_qs = function(url, table) { var parts = url.split('?'); var vars = []; if (parts.length == 2) { var name_value_pairs = parts[1].split('&'); for (var i = 0; i < name_value_pairs.length; i++) { var name = name_value_pairs[i].split('=')[0]; if (name && !table.hasOwnProperty(name)) { vars.push(name_value_pairs[i]); } } } for (var key in table) { vars.push(key + '=' + this._cast_to_string(table[key])); } return parts[0] + '?' + vars.join('&'); }; }