Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
google-analytics-django
Advanced tools
A comprehensive Django package for seamless integration of Google Analytics into your Django projects, supporting Google Analytics 4 (GA4).
pip install google-analytics-django
INSTALLED_APPS = [
# ...
'google_analytics_django',
# ...
]
MIDDLEWARE = [
# ...
'google_analytics_django.middleware.GoogleAnalyticsMiddleware',
# ...
]
Add the following settings to your Django project's settings.py file:
# Required
GOOGLE_ANALYTICS_PROPERTY_ID = 'UA-XXXXXXXX-X' # or 'G-XXXXXXXXXX' for GA4
# Optional (shown with default values)
GOOGLE_ANALYTICS_DOMAIN = 'auto'
GOOGLE_ANALYTICS_ANONYMIZE_IP = False
GOOGLE_ANALYTICS_SAMPLE_RATE = 100
GOOGLE_ANALYTICS_SITE_SPEED_SAMPLE_RATE = 1
GOOGLE_ANALYTICS_COOKIE_EXPIRES = 63072000 # 2 years in seconds
GOOGLE_ANALYTICS_DISPLAY_FEATURES = False
GOOGLE_ANALYTICS_USE_GTAG = True # Set to False to use analytics.js
GOOGLE_ANALYTICS_DEBUG_MODE = False
In your base template, load the template tags and add the Google Analytics script:
{% load google_analytics_tags %}
<head>
<!-- ... other head elements ... -->
{% google_analytics %}
</head>
The middleware is automatically active once added to your MIDDLEWARE setting. It adds two custom headers to the response:
X-GA-TRACKING-ID
: The Google Analytics client ID (if available)X-CLIENT-IP
: The client's IP addressThese headers can be used for server-side tracking if needed.
Set GOOGLE_ANALYTICS_DEBUG_MODE = True
in your settings to prevent tracking in development environments.
By default, staff users (users with is_staff=True
) are not tracked. You can modify this behavior in the should_track
function in utils.py
.
Use the standard Google Analytics JavaScript API to track events:
gtag("event", "button_click", {
event_category: "engagement",
event_label: "hero_cta",
});
Set up custom dimensions and metrics in your Google Analytics property, then use them in your tracking code:
gtag("config", "UA-XXXXXXXX-X", {
custom_map: { dimension1: "user_type" },
});
gtag("event", "page_view", { user_type: "member" });
If you encounter issues:
GOOGLE_ANALYTICS_PROPERTY_ID
is correct.Contributions are welcome! Please follow these steps:
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)Please ensure your code adheres to the project's coding standards and include tests for new features.
This project is licensed under the MIT License - see the LICENSE file for details.
If you need help or have any questions, please open an issue on the GitHub repository or contact the maintainers.
FAQs
A Django package to integrate Google Analytics seamlessly.
We found that google-analytics-django demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.