def test_ga_template_tag(self): rf = RequestFactory() post_request = rf.post('/submit/', {'foo': 'bar'}) url = google_analytics( {'request': post_request}, tracking_code='ua-test-id', debug=True) self.assertEqual(parse_qs(url).get('tracking_code'), ['ua-test-id']) self.assertEqual(parse_qs(url).get('utmdebug'), ['1']) url = google_analytics( {'request': post_request}, tracking_code='ua-test-id', debug=False) self.assertEqual(parse_qs(url).get('utmdebug'), None)
def test_ga_template_tag(self): rf = RequestFactory() post_request = rf.post('/submit/', {'foo': 'bar'}) url = google_analytics({'request': post_request}, tracking_code='ua-test-id', debug=True) self.assertEqual(parse_qs(url).get('tracking_code'), ['ua-test-id']) self.assertEqual(parse_qs(url).get('utmdebug'), ['1']) url = google_analytics({'request': post_request}, tracking_code='ua-test-id', debug=False) self.assertEqual(parse_qs(url).get('utmdebug'), None)
def add_analytics_scripts(context): analytics_string = """ <!-- in debug mode, analytics disabled --> <script> window.disableAnalytics = function(){ console.log("disableAnalytics (called in debug mode)"); }; window.enableAnalytics = function(){ console.log("enableAnalytics (called in debug mode)"); }; </script> """ if not settings.DEBUG: analytics_string = """ <div style="display:none"> <!-- server side analytics --> <img src=\"""" + google_analytics( context) + """\"width="0" height="0" /> </div> <script async src="https://www.googletagmanager.com/gtag/js?id=""" + settings.ANALYTICS_ID + """\"></script> <script> var useHotjar = function(h,o,t,j,a,r){ h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)}; h._hjSettings={hjid:1526596,hjsv:6}; a=o.getElementsByTagName('head')[0]; r=o.createElement('script');r.async=1; r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv; a.appendChild(r); }; var gtagId = '""" + settings.ANALYTICS_ID + """'; window.disableAnalytics = function(){ window['ga-disable-' + gtagId] = true; }; window.enableAnalytics = function(){ //window['ga-disable-' + gtagId] = false; gtag('config', gtagId); useHotjar(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv='); }; window.disableAnalytics(); window.dataLayer = window.dataLayer || []; function gtag(){ dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', gtagId); </script> """ return mark_safe(analytics_string)
def test_template_blank_if_tracking_code_unset(self): context = Context({'request': self.request}) rendered_template = google_analytics(context) self.assertEqual('', rendered_template)
def test_tracking_code_defaults_to_settings(self): context = Context({'request': self.request}) rendered_template = google_analytics(context) self.assertIn('tracking_code=ua-test-id', rendered_template)
def test_tracking_code_used_if_passed_in(self): context = Context({'request': self.request}) rendered_template = google_analytics(context, tracking_code='my-code') self.assertIn('tracking_code=my-code', rendered_template)
def test_template_blank_if_tracking_code_unset(self): context = Context({'request': self.request}) rendered_template = google_analytics(context) self.assertEqual('', rendered_template)
def test_tracking_code_defaults_to_settings(self): context = Context({'request': self.request}) rendered_template = google_analytics(context) self.assertIn('tracking_code=ua-test-id', rendered_template)
def test_tracking_code_used_if_passed_in(self): context = Context({'request': self.request}) rendered_template = google_analytics(context, tracking_code='my-code') self.assertIn('tracking_code=my-code', rendered_template)