1616@override_settings (GOOGLE_ANALYTICS_GTAG_PROPERTY_ID = 'UA-123456-7' )
1717class GoogleAnalyticsTagTestCase (TagTestCase ):
1818 """
19- Tests for the ``google_analytics_js `` template tag.
19+ Tests for the ``google_analytics_gtag `` template tag.
2020 """
2121
2222 def test_tag (self ):
@@ -25,15 +25,15 @@ def test_tag(self):
2525 '<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456-7"></script>'
2626 ) in r
2727 assert "gtag('js', new Date());" in r
28- assert "gtag('config', 'UA-123456-7');" in r
28+ assert "gtag('config', 'UA-123456-7', {} );" in r
2929
3030 def test_node (self ):
3131 r = GoogleAnalyticsGTagNode ().render (Context ())
3232 assert (
3333 '<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456-7"></script>'
3434 ) in r
3535 assert "gtag('js', new Date());" in r
36- assert "gtag('config', 'UA-123456-7');" in r
36+ assert "gtag('config', 'UA-123456-7', {} );" in r
3737
3838 @override_settings (GOOGLE_ANALYTICS_GTAG_PROPERTY_ID = None )
3939 def test_no_property_id (self ):
@@ -57,7 +57,7 @@ def test_render_internal_ip(self):
5757 @override_settings (ANALYTICAL_AUTO_IDENTIFY = True )
5858 def test_identify (self ):
5959 r = GoogleAnalyticsGTagNode ().render (Context ({'user' : User (username = 'test' )}))
60- assert " gtag('set ', {'user_id': ' test' });" in r
60+ assert ' gtag(\' config \ ' , \' UA-123456-7 \' , {"user_id": " test" });' in r
6161
6262 def test_identity_context_specific_provider (self ):
6363 """
@@ -68,12 +68,13 @@ def test_identity_context_specific_provider(self):
6868 Context (
6969 {
7070 'google_analytics_gtag_identity' : 'foo_gtag_identity' ,
71- 'analytical_identity' : 'bar_analytical_identity' ,
7271 'user' : User (username = 'test' ),
7372 }
7473 )
7574 )
76- assert "gtag('set', {'user_id': 'foo_gtag_identity'});" in r
75+ assert (
76+ 'gtag(\' config\' , \' UA-123456-7\' , {"user_id": "foo_gtag_identity"});' in r
77+ )
7778
7879 def test_identity_context_general (self ):
7980 """
@@ -87,7 +88,10 @@ def test_identity_context_general(self):
8788 }
8889 )
8990 )
90- assert "gtag('set', {'user_id': 'bar_analytical_identity'});" in r
91+ assert (
92+ 'gtag(\' config\' , \' UA-123456-7\' , {"user_id": "bar_analytical_identity"});'
93+ in r
94+ )
9195
9296 @override_settings (GOOGLE_ANALYTICS_GTAG_PROPERTY_ID = 'G-12345678' )
9397 def test_tag_with_measurement_id (self ):
@@ -96,7 +100,7 @@ def test_tag_with_measurement_id(self):
96100 '<script async src="https://www.googletagmanager.com/gtag/js?id=G-12345678"></script>'
97101 ) in r
98102 assert "gtag('js', new Date());" in r
99- assert "gtag('config', 'G-12345678');" in r
103+ assert "gtag('config', 'G-12345678', {} );" in r
100104
101105 @override_settings (GOOGLE_ANALYTICS_GTAG_PROPERTY_ID = 'AW-1234567890' )
102106 def test_tag_with_conversion_id (self ):
@@ -105,7 +109,7 @@ def test_tag_with_conversion_id(self):
105109 '<script async src="https://www.googletagmanager.com/gtag/js?id=AW-1234567890"></script'
106110 ) in r
107111 assert "gtag('js', new Date());" in r
108- assert "gtag('config', 'AW-1234567890');" in r
112+ assert "gtag('config', 'AW-1234567890', {} );" in r
109113
110114 @override_settings (GOOGLE_ANALYTICS_GTAG_PROPERTY_ID = 'DC-12345678' )
111115 def test_tag_with_advertiser_id (self ):
@@ -114,4 +118,47 @@ def test_tag_with_advertiser_id(self):
114118 '<script async src="https://www.googletagmanager.com/gtag/js?id=DC-12345678"></script>'
115119 ) in r
116120 assert "gtag('js', new Date());" in r
117- assert "gtag('config', 'DC-12345678');" in r
121+ assert "gtag('config', 'DC-12345678', {});" in r
122+
123+ def test_tag_with_custom_dimensions (self ):
124+ r = GoogleAnalyticsGTagNode ().render (
125+ Context (
126+ {
127+ 'google_analytics_custom_dimensions' : {
128+ 'dimension_1' : 'foo' ,
129+ 'dimension_2' : 'bar' ,
130+ 'user_properties' : {
131+ 'user_property_1' : True ,
132+ 'user_property_2' : 'xyz' ,
133+ },
134+ },
135+ }
136+ )
137+ )
138+ assert (
139+ "gtag('config', 'UA-123456-7', {"
140+ '"dimension_1": "foo", '
141+ '"dimension_2": "bar", '
142+ '"user_properties": {'
143+ '"user_property_1": true, '
144+ '"user_property_2": "xyz"}});' in r
145+ )
146+
147+ def test_tag_with_identity_and_custom_dimensions (self ):
148+ r = GoogleAnalyticsGTagNode ().render (
149+ Context (
150+ {
151+ 'google_analytics_gtag_identity' : 'foo_gtag_identity' ,
152+ 'google_analytics_custom_dimensions' : {
153+ 'dimension_1' : 'foo' ,
154+ 'dimension_2' : 'bar' ,
155+ },
156+ }
157+ )
158+ )
159+ assert (
160+ "gtag('config', 'UA-123456-7', {"
161+ '"dimension_1": "foo", '
162+ '"dimension_2": "bar", '
163+ '"user_id": "foo_gtag_identity"});' in r
164+ )
0 commit comments