def render(self, context): custom_variables = context.get('piwik_vars', ()) complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE, ) for var in custom_variables) variables_code = (VARIABLE_CODE % PiwikVar(*var)._asdict() for var in complete_variables) commands = [] if getattr(settings, 'PIWIK_DISABLE_COOKIES', False): commands.append(DISABLE_COOKIES_CODE) userid = get_identity(context, 'piwik') if userid is not None: variables_code = chain(variables_code, (IDENTITY_CODE % { 'userid': userid }, )) html = TRACKING_CODE % { 'url': self.domain_path, 'siteid': self.site_id, 'variables': '\n '.join(variables_code), 'commands': '\n '.join(commands) } if is_internal_ip(context, 'PIWIK'): html = disable_html(html, 'Piwik') return html
def render(self, context): commands = self._get_custom_var_commands(context) html = SETUP_CODE % {'property_id': self.property_id, 'commands': " ".join(commands)} if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = TRACKING_CODE % {'portal_id': self.portal_id} if is_internal_ip(context, 'HUBSPOT'): html = disable_html(html, 'HubSpot') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = SETUP_CODE % {'account_number': self.account_number} if is_internal_ip(context, 'OPTIMIZELY'): html = disable_html(html, 'Optimizely') return html
def render(self, context): custom_variables = context.get('matomo_vars', ()) complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) for var in custom_variables) variables_code = (VARIABLE_CODE % MatomoVar(*var)._asdict() for var in complete_variables) commands = [] if getattr(settings, 'MATOMO_DISABLE_COOKIES', False): commands.append(DISABLE_COOKIES_CODE) userid = get_identity(context, 'matomo') if userid is not None: variables_code = chain(variables_code, ( IDENTITY_CODE % {'userid': userid}, )) # this works if using csp_nonce.middleware.CSPNonceMiddleware if hasattr(context, 'request') and hasattr(context.request, 'script_nonce'): nonce = 'nonce={0}'.format(context.request.script_nonce) else: nonce = '' html = TRACKING_CODE % { 'url': self.domain_path, 'siteid': self.site_id, 'variables': '\n '.join(variables_code), 'commands': '\n '.join(commands), 'nonce': nonce } if is_internal_ip(context, 'MATOMO'): html = disable_html(html, 'Matomo') return html
def render(self, context): commands = [] identity = get_identity(context, 'kiss_metrics') if identity is not None: commands.append(IDENTIFY_CODE % identity) try: properties = context[ALIAS_CONTEXT_KEY] key, value = properties.popitem() commands.append(ALIAS_CODE % (key, value)) except KeyError: pass try: name, properties = context[EVENT_CONTEXT_KEY] commands.append(EVENT_CODE % { 'name': name, 'properties': json.dumps(properties, sort_keys=True), }) except KeyError: pass try: properties = context[PROPERTY_CONTEXT_KEY] commands.append(PROPERTY_CODE % { 'properties': json.dumps(properties, sort_keys=True), }) except KeyError: pass html = TRACKING_CODE % { 'api_key': self.api_key, 'commands': " ".join(commands), } if is_internal_ip(context, 'KISS_METRICS'): html = disable_html(html, 'KISSmetrics') return html
def render(self, context): commands = [] identity = get_identity(context, 'kiss_metrics') if identity is not None: commands.append(IDENTIFY_CODE % identity) try: properties = context[ALIAS_CONTEXT_KEY] key, value = properties.popitem() commands.append(ALIAS_CODE % (key, value)) except KeyError: pass try: name, properties = context[EVENT_CONTEXT_KEY] commands.append(EVENT_CODE % {'name': name, 'properties': json.dumps(properties, sort_keys=True)}) except KeyError: pass try: properties = context[PROPERTY_CONTEXT_KEY] commands.append(PROPERTY_CODE % { 'properties': json.dumps(properties, sort_keys=True)}) except KeyError: pass html = TRACKING_CODE % {'api_key': self.api_key, 'commands': " ".join(commands)} if is_internal_ip(context, 'KISS_METRICS'): html = disable_html(html, 'KISSmetrics') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = SETUP_CODE.format(property_id=self.property_id, ) if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): html = COUNTER_CODE % { 'counter_id': self.counter_id, } if is_internal_ip(context, 'RATING_MAILRU_METRICA'): html = disable_html(html, '*****@*****.**') return html
def render(self, context): commands = [] identity = get_identity(context, 'mixpanel') if identity is not None: if isinstance(identity, dict): commands.append(IDENTIFY_CODE % identity.get('id', identity.get('username'))) commands.append(IDENTIFY_PROPERTIES % json.dumps(identity, sort_keys=True)) else: commands.append(IDENTIFY_CODE % identity) try: name, properties = context[EVENT_CONTEXT_KEY] commands.append( EVENT_CODE % { 'name': name, 'properties': json.dumps(properties, sort_keys=True) }) except KeyError: pass html = TRACKING_CODE % { 'token': self.token, 'commands': " ".join(commands) } if is_internal_ip(context, 'MIXPANEL'): html = disable_html(html, 'Mixpanel') return html
def render(self, context): html = SETUP_CODE.format( property_id=self.property_id, ) if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): custom_variables = context.get('matomo_vars', ()) complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) for var in custom_variables) variables_code = (VARIABLE_CODE % MatomoVar(*var)._asdict() for var in complete_variables) commands = [] if getattr(settings, 'MATOMO_DISABLE_COOKIES', False): commands.append(DISABLE_COOKIES_CODE) userid = get_identity(context, 'matomo') if userid is not None: variables_code = chain(variables_code, ( IDENTITY_CODE % {'userid': userid}, )) html = TRACKING_CODE % { 'url': self.domain_path, 'siteid': self.site_id, 'variables': '\n '.join(variables_code), 'commands': '\n '.join(commands) } if is_internal_ip(context, 'MATOMO'): html = disable_html(html, 'Matomo') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = TRACKING_CODE % {'tracker_id': self.tracker_id} if is_internal_ip(context, 'CLICKMAP'): html = disable_html(html, 'Clickmap') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = TRACKING_CODE % {'site_id': self.site_id} if is_internal_ip(context, 'GAUGES'): html = disable_html(html, 'Gauges') return html
def render(self, context): html = LUCKYORANGE_TRACKING_CODE % { 'LUCKYORANGE_SITE_ID': self.site_id } if is_internal_ip(context, 'LUCKYORANGE'): return disable_html(html, 'Lucky Orange') else: return html
def render(self, context): html = TRACKING_CODE % { 'url': self.domain_path, 'siteid': self.site_id, } if is_internal_ip(context, 'PIWIK'): html = disable_html(html, 'Piwik') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = self.code_template % {'FACEBOOK_PIXEL_ID': self.pixel_id} if is_internal_ip(context, 'FACEBOOK_PIXEL'): return disable_html(html, 'Facebook Pixel') else: return html
def render(self, context): html = TRACKING_CODE % { 'portal_id': self.portal_id, 'domain': self.domain } if is_internal_ip(context, 'HUBSPOT'): html = disable_html(html, 'HubSpot') return html
def render(self, context): html = TRACKING_CODE % { 'portal_id': self.portal_id, 'domain': self.domain } if is_internal_ip(context, 'CLICKMAP'): html = disable_html(html, 'Clickmap') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = HOTJAR_TRACKING_CODE % {'HOTJAR_SITE_ID': self.site_id} if is_internal_ip(context, 'HOTJAR'): return disable_html(html, 'Hotjar') else: return html
def render(self, context): html = SETUP_CODE % {'api_key': self.api_key} identity = get_identity(context, 'performable') if identity is not None: html = "%s%s" % (IDENTIFY_CODE % identity, html) if is_internal_ip(context, 'PERFORMABLE'): html = disable_html(html, 'Performable') return html
def render(self, context): commands = self._get_domain_commands(context) commands.extend(self._get_custom_var_commands(context)) commands.extend(self._get_other_commands(context)) html = SETUP_CODE % {'property_id': self.property_id, 'commands': " ".join(commands)} if is_internal_ip(context, 'GOOGLE_ANALYTICS') or on_debug_mode(): html = disable_html(html, 'Google Analytics') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = COUNTER_CODE % { 'counter_id': self.counter_id, } if is_internal_ip(context, 'RATING_MAILRU_METRICA'): html = disable_html(html, '*****@*****.**') return html
def render(self, context): config = {'uid': self.user_id} domain = _get_domain(context) if domain is not None: config['domain'] = domain html = SETUP_CODE % {'config': json.dumps(config, sort_keys=True)} if is_internal_ip(context, 'CHARTBEAT'): html = disable_html(html, 'Chartbeat') return html
def render(self, context): commands = self._get_domain_commands(context) commands.extend(self._get_custom_var_commands(context)) commands.extend(self._get_other_commands(context)) html = SETUP_CODE % {'property_id': self.property_id, 'commands': " ".join(commands)} if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): user = get_user_from_context(context) params = self._get_custom_attrs(context) params["app_id"] = self.app_id html = TRACKING_CODE % {"settings_json": json.dumps(params, sort_keys=True)} if is_internal_ip(context, "INTERCOM") or not user or not user.is_authenticated(): # Intercom is disabled for non-logged in users. html = disable_html(html, "Intercom") return html
def render(self, context): params = self._get_custom_attrs(context) params["app_id"] = self.app_id html = TRACKING_CODE % { "settings_json": json.dumps(params, sort_keys=True) } if is_internal_ip(context, 'INTERCOM'): html = disable_html(html, 'Intercom') return html
def render(self, context): if settings.get("DISABLE_TRACKING_CODE", False): return "" html = SETUP_CODE % {'api_key': self.api_key} identity = get_identity(context, 'performable') if identity is not None: html = "%s%s" % (IDENTIFY_CODE % identity, html) if is_internal_ip(context, 'PERFORMABLE'): html = disable_html(html, 'Performable') return html
def render(self, context): settings = self._get_settings(context) visitor = self._get_visitor(context) html = TRACKING_CODE % { 'settings': json.dumps(settings, sort_keys=True), 'visitor': json.dumps(visitor, sort_keys=True), } if is_internal_ip(context, 'WOOPRA'): html = disable_html(html, 'Woopra') return html
def render(self, context): html = "" user = get_user_from_context(context) vars = self._get_custom_attrs(context) vars["app_id"] = self.app_id html = TRACKING_CODE % {"settings_json": json.dumps(vars, sort_keys=True)} if is_internal_ip(context, 'INTERCOM') or not user or not user.is_authenticated(): # Intercom is disabled for non-logged in users. html = disable_html(html, 'Intercom') return html
def render(self, context): settings = self._get_settings(context) visitor = self._get_visitor(context) html = TRACKING_CODE % { 'settings': simplejson.dumps(settings), 'visitor': simplejson.dumps(visitor), } if is_internal_ip(context, 'WOOPRA'): html = disable_html(html, 'Woopra') return html
def render(self, context): commands = self._get_domain_commands(context) commands.extend(self._get_custom_var_commands(context)) commands.extend(self._get_other_commands(context)) commands.append(SEND_PAGEVIEW_CODE) html = SETUP_CODE % { 'commands': " ".join(commands), } if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): configs = [TOKEN_CODE % self.site_token] identity = get_identity(context, 'gosquared', self._identify) if identity: configs.append(IDENTIFY_CODE % identity) html = TRACKING_CODE % { 'token': self.site_token, 'config': ' '.join(configs), } if is_internal_ip(context, 'GOSQUARED'): html = disable_html(html, 'GoSquared') return html
def render(self, context): pretrack = _get_additional_calls(context) html = TRACKING_CODE % { 'method': "['trackPageView']", 'url': self.domain_path, 'siteid': self.site_id, 'pretrack': pretrack, } if is_internal_ip(context, 'PIWIK'): html = disable_html(html, 'Piwik') return html
def render(self, context): html = SETUP_CODE % {'account_nr_1': self.account_nr[:4], 'account_nr_2': self.account_nr[4:]} values = (context.get('crazy_egg_var%d' % i) for i in range(1, 6)) vars = [(i, v) for i, v in enumerate(values, 1) if v is not None] if vars: js = " ".join(USERVAR_CODE % {'varnr': varnr, 'value': value} for (varnr, value) in vars) html = '%s\n<script type="text/javascript">%s</script>' \ % (html, js) if is_internal_ip(context, 'CRAZY_EGG'): html = disable_html(html, 'Crazy Egg') return html
def render(self, context): commands = self._get_domain_commands(context) commands.extend(self._get_custom_var_commands(context)) commands.extend(self._get_other_commands(context)) if getattr(settings, 'GOOGLE_ANALYTICS_DISPLAY_ADVERTISING', False): source = DISPLAY_ADVERTISING_SOURCE else: source = DEFAULT_SOURCE html = SETUP_CODE % {'property_id': self.property_id, 'commands': " ".join(commands), 'source_scheme': source[0], 'source_url': source[1]} if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): custom = {} for dict_ in context: for var, val in dict_.items(): if var.startswith('clicky_'): custom[var[7:]] = val if 'username' not in custom.get('session', {}): identity = get_identity(context, 'clicky') if identity is not None: custom.setdefault('session', {})['username'] = identity html = TRACKING_CODE % {'site_id': self.site_id, 'custom': simplejson.dumps(custom)} if is_internal_ip(context, 'CLICKY'): html = disable_html(html, 'Clicky') return html
def render(self, context): commands = [] identity = get_identity(context, 'kiss_metrics') if identity is not None: commands.append(IDENTIFY_CODE % identity) try: name, properties = context[EVENT_CONTEXT_KEY] commands.append(EVENT_CODE % {'name': name, 'properties': simplejson.dumps(properties)}) except KeyError: pass html = TRACKING_CODE % {'api_key': self.api_key, 'commands': " ".join(commands)} if is_internal_ip(context, 'KISS_METRICS'): html = disable_html(html, 'KISSmetrics') return html
def render(self, context): commands = [] identity = get_identity(context, 'mixpanel') if identity is not None: commands.append(IDENTIFY_CODE % identity) try: name, properties = context[EVENT_CONTEXT_KEY] commands.append(EVENT_CODE % {'name': name, 'properties': simplejson.dumps(properties)}) except KeyError: pass html = TRACKING_CODE % {'token': self.token, 'commands': " ".join(commands)} if is_internal_ip(context, 'MIXPANEL'): html = disable_html(html, 'Mixpanel') return html
def render(self, context): import json create_fields = self._get_domain_fields(context) create_fields.update(self._get_other_create_fields(context)) commands = self._get_custom_var_commands(context) commands.extend(self._get_other_commands(context)) display_features = getattr(settings, 'GOOGLE_ANALYTICS_DISPLAY_ADVERTISING', False) html = SETUP_CODE.format( property_id=self.property_id, create_fields=json.dumps(create_fields), display_features=REQUIRE_DISPLAY_FEATURES if display_features else '', commands=" ".join(commands), ) if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html
def render(self, context): custom = {} for dict_ in context: for var, val in dict_.items(): if var.startswith('spring_metrics_'): custom[var[15:]] = val if 'email' not in custom: identity = get_identity(context, 'spring_metrics', lambda u: u.email) if identity is not None: custom['email'] = identity html = TRACKING_CODE % {'tracking_id': self.tracking_id, 'custom_commands': self._generate_custom_javascript(custom)} if is_internal_ip(context, 'SPRING_METRICS'): html = disable_html(html, 'Spring Metrics') return html
def render(self, context): custom_variables = context.get("piwik_vars", ()) complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) for var in custom_variables) variables_code = (VARIABLE_CODE % PiwikVar(*var)._asdict() for var in complete_variables) userid = get_identity(context, "piwik") if userid is not None: variables_code = chain(variables_code, (IDENTITY_CODE % {"userid": userid},)) html = TRACKING_CODE % { "url": self.domain_path, "siteid": self.site_id, "variables": "\n ".join(variables_code), } if is_internal_ip(context, "PIWIK"): html = disable_html(html, "Piwik") return html
def render(self, context): re_vars = {} for dict_ in context: for var, val in dict_.items(): if var.startswith("reinvigorate_"): re_vars[var[13:]] = val if "name" not in re_vars: identity = get_identity(context, "reinvigorate", lambda u: u.get_full_name()) if identity is not None: re_vars["name"] = identity if "context" not in re_vars: email = get_identity(context, "reinvigorate", lambda u: u.email) if email is not None: re_vars["context"] = email tags = " ".join("var re_%s_tag = %s;" % (tag, simplejson.dumps(value)) for tag, value in re_vars.items()) html = TRACKING_CODE % {"tracking_id": self.tracking_id, "tags": tags} if is_internal_ip(context, "REINVIGORATE"): html = disable_html(html, "Reinvigorate") return html
def render(self, context): commands = [] identity = get_identity(context, 'mixpanel') if identity is not None: if isinstance(identity, dict): commands.append(IDENTIFY_CODE % identity.get('id', identity.get('username'))) commands.append(IDENTIFY_PROPERTIES % json.dumps(identity, sort_keys=True)) else: commands.append(IDENTIFY_CODE % identity) try: name, properties = context[EVENT_CONTEXT_KEY] commands.append(EVENT_CODE % {'name': name, 'properties': json.dumps(properties, sort_keys=True)}) except KeyError: pass html = TRACKING_CODE % {'token': self._token, 'commands': " ".join(commands)} if is_internal_ip(context, 'MIXPANEL'): html = disable_html(html, 'Mixpanel') return mark_safe(html)
def render(self, context): options = { 'id': int(self.counter_id), 'clickmap': True, 'trackLinks': True, 'accurateTrackBounce': True } if getattr(settings, 'YANDEX_METRICA_WEBVISOR', False): options['webvisor'] = True if getattr(settings, 'YANDEX_METRICA_TRACKHASH', False): options['trackHash'] = True if getattr(settings, 'YANDEX_METRICA_NOINDEX', False): options['ut'] = 'noindex' if getattr(settings, 'YANDEX_METRICA_ECOMMERCE', False): options['ecommerce'] = 'dataLayer' html = COUNTER_CODE % { 'counter_id': self.counter_id, 'options': json.dumps(options), } if is_internal_ip(context, 'YANDEX_METRICA'): html = disable_html(html, 'Yandex.Metrica') return html
def render(self, context): re_vars = {} for dict_ in context: for var, val in dict_.items(): if var.startswith('reinvigorate_'): re_vars[var[13:]] = val if 'name' not in re_vars: identity = get_identity(context, 'reinvigorate', lambda u: u.get_full_name()) if identity is not None: re_vars['name'] = identity if 'context' not in re_vars: email = get_identity(context, 'reinvigorate', lambda u: u.email) if email is not None: re_vars['context'] = email tags = " ".join("var re_%s_tag = %s;" % (tag, json.dumps(value, sort_keys=True)) for tag, value in re_vars.items()) html = TRACKING_CODE % {'tracking_id': self.tracking_id, 'tags': tags} if is_internal_ip(context, 'REINVIGORATE'): html = disable_html(html, 'Reinvigorate') return html
def render(self, context): custom_variables = context.get('piwik_vars', ()) complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) for var in custom_variables) variables_code = (VARIABLE_CODE % PiwikVar(*var)._asdict() for var in complete_variables) userid = get_identity(context, 'piwik') if userid is not None: variables_code = chain(variables_code, ( IDENTITY_CODE % {'userid': userid}, )) html = TRACKING_CODE % { 'url': self.domain_path, 'siteid': self.site_id, 'variables': '\n '.join(variables_code) } if is_internal_ip(context, 'PIWIK'): html = disable_html(html, 'Piwik') return html
def test_render_internal_ip_forwarded_for(self): req = HttpRequest() req.META['HTTP_X_FORWARDED_FOR'] = '1.1.1.1' context = Context({'request': req}) self.assertTrue(is_internal_ip(context))
def test_render_no_internal_ip(self): context = Context() self.assertFalse(is_internal_ip(context))
def test_render_prefix_internal_ip(self): req = HttpRequest() req.META['REMOTE_ADDR'] = '1.1.1.1' context = Context({'request': req}) self.assertTrue(is_internal_ip(context, 'TEST'))
def test_render_internal_ip_fallback(self): req = HttpRequest() req.META['REMOTE_ADDR'] = '1.1.1.1' context = Context({'request': req}) self.assertTrue(is_internal_ip(context))
def test_render_different_internal_ip(self): req = HttpRequest() req.META['REMOTE_ADDR'] = '2.2.2.2' context = Context({'request': req}) self.assertFalse(is_internal_ip(context))