Updated Gtag implementation for mutiple id patterns

This commit is contained in:
Taha Rushain 2020-07-17 19:24:51 +05:00
parent ac2ebf375c
commit 3292b664ec
3 changed files with 35 additions and 2 deletions

View file

@ -15,7 +15,7 @@ from analytical.utils import (
is_internal_ip,
)
PROPERTY_ID_RE = re.compile(r'^UA-\d+-\d+$|^G-[a-zA-Z0-9]+$')
PROPERTY_ID_RE = re.compile(r'^UA-\d+-\d+$|^G-[a-zA-Z0-9]+$|^AW-[a-zA-Z0-9]+$|^DC-[a-zA-Z0-9]+$')
SETUP_CODE = """
<script async src="https://www.googletagmanager.com/gtag/js?id={property_id}"></script>
<script>
@ -52,7 +52,7 @@ class GoogleAnalyticsGTagNode(Node):
def __init__(self):
self.property_id = get_required_setting(
'GOOGLE_ANALYTICS_GTAG_PROPERTY_ID', PROPERTY_ID_RE,
"must be a string looking like 'UA-XXXXXX-Y'")
"must be a string looking like one of these patterns ('UA-XXXXXX-Y' , 'AW-XXXXXXXXXX', 'G-XXXXXXXX', 'DC-XXXXXXXX')")
def render(self, context):
other_fields = {}

View file

@ -56,3 +56,30 @@ class GoogleAnalyticsTagTestCase(TagTestCase):
def test_identify(self):
r = GoogleAnalyticsGTagNode().render(Context({'user': User(username='test')}))
self.assertTrue("gtag('set', {'user_id': 'test'});" in r, r)
@override_settings(GOOGLE_ANALYTICS_GTAG_PROPERTY_ID='G-12345678')
def test_tag_with_measurement_id(self):
r = self.render_tag('google_analytics_gtag', 'google_analytics_gtag')
self.assertTrue(
'<script async src="https://www.googletagmanager.com/gtag/js?id=G-12345678"></script>'
in r, r)
self.assertTrue("gtag('js', new Date());" in r, r)
self.assertTrue("gtag('config', 'G-12345678');" in r, r)
@override_settings(GOOGLE_ANALYTICS_GTAG_PROPERTY_ID='AW-1234567890')
def test_tag_with_conversion_id(self):
r = self.render_tag('google_analytics_gtag', 'google_analytics_gtag')
self.assertTrue(
'<script async src="https://www.googletagmanager.com/gtag/js?id=AW-1234567890"></script>'
in r, r)
self.assertTrue("gtag('js', new Date());" in r, r)
self.assertTrue("gtag('config', 'AW-1234567890');" in r, r)
@override_settings(GOOGLE_ANALYTICS_GTAG_PROPERTY_ID='DC-12345678')
def test_tag_with_advertiser_id(self):
r = self.render_tag('google_analytics_gtag', 'google_analytics_gtag')
self.assertTrue(
'<script async src="https://www.googletagmanager.com/gtag/js?id=DC-12345678"></script>'
in r, r)
self.assertTrue("gtag('js', new Date());" in r, r)
self.assertTrue("gtag('config', 'DC-12345678');" in r, r)

View file

@ -69,6 +69,12 @@ project :file:`settings.py` file::
If you do not set a property ID, the tracking code will not be rendered.
Please node that the accepted Property IDs should be one of the following formats:
- 'UA-XXXXXX-Y'
- 'AW-XXXXXXXXXX'
- 'G-XXXXXXXX'
- 'DC-XXXXXXXX'
Internal IP addresses