Merge pull request #164 from taharushain/master

Updated regex for gtag to accept measurement ID
This commit is contained in:
Joost Cassee 2020-08-02 12:39:40 +02:00 committed by GitHub
commit ef40ba6ff5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 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+$')
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,9 @@ 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,33 @@ 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