让谷歌分析 ID 成为一个变量

发布于 2024-12-07 02:21:07 字数 1800 浏览 1 评论 0原文

我的应用程序服务于多个域,我理解这些域应该由我正在研究的命名空间来完成。由于多个域应该有多个分析 ID:我从代码中获取分析 ID,但我希望使其更加可配置:

    if os.environ.get('HTTP_HOST').endswith('.br') \
        or os.environ['SERVER_NAME'].endswith('.br'):
        data[u'analytics'] = 'UA-637933-12'
else:
        data[u'analytics'] = 'UA-637933-18'

    self.response.out.write(template.render(os.path.join(os.path.dirname(__file__),
                            'templates', name + '.html'), data))

如果是我的巴西域,则上面将分析 ID 设置为 ..-12,并将其他 ID 设置为 .. .-18(如果是我的 .com)。但这仅适用于 2 个域,并且不容易推广。如何以更科学、更可扩展的方式实现这一功能,以便轻松地将我的应用程序添加到域中,而无需手动将域添加到我的应用程序中?

我想命名空间是去这里的方法,因为域是谷歌应用程序域,但我不明白如何使用命名空间

def namespace_manager_default_namespace_for_request():
  """Determine which namespace is to be used for a request.

  The value of _NAMESPACE_PICKER has the following effects:

  If _USE_SERVER_NAME, we read server name
  foo.guestbook-isv.appspot.com and set the namespace.

  If _USE_GOOGLE_APPS_DOMAIN, we allow the namespace manager to infer
  the namespace from the request.

  If _USE_COOKIE, then the ISV might have a gateway page that sets a
  cookie called 'namespace', and we set the namespace to the cookie's value
  """
  name = None

  if _NAMESPACE_PICKER == _USE_SERVER_NAME:
    name = os.environ['SERVER_NAME']
  elif _NAMESPACE_PICKER == _USE_GOOGLE_APPS_DOMAIN:
    name = namespace_manager.google_apps_namespace()
  elif _NAMESPACE_PICKER == _USE_COOKIE:
    cookies = os.environ.get('HTTP_COOKIE', None)
    if cookies:
      name = Cookie.BaseCookie(cookies).get('namespace')

  return name

我想我应该使用命名空间管理器,获取命名空间并根据命名空间设置分析 ID,但是如何呢?

谢谢

My app serves multiple domains which I understand should be done by namespaces which I'm researching. Since multiple domains should have multiple analytics ID:s I get the analytics ID from the code but I want to make it even more configurable:

    if os.environ.get('HTTP_HOST').endswith('.br') \
        or os.environ['SERVER_NAME'].endswith('.br'):
        data[u'analytics'] = 'UA-637933-12'
else:
        data[u'analytics'] = 'UA-637933-18'

    self.response.out.write(template.render(os.path.join(os.path.dirname(__file__),
                            'templates', name + '.html'), data))

The above sets analytics ID to ..-12 if it's my brazilian domain and to the other ID ...-18 if it is my dot com. But this is only for 2 domains and it's not easiliy generalizable. How can I achieve this function in a more scientific and scalable way so that it becomes easy to add my application to a domain without manually adding the domain to my application?

I suppose namespaces is the way to go here since the domains are google apps domains but I don't understand how to use namespaces:

def namespace_manager_default_namespace_for_request():
  """Determine which namespace is to be used for a request.

  The value of _NAMESPACE_PICKER has the following effects:

  If _USE_SERVER_NAME, we read server name
  foo.guestbook-isv.appspot.com and set the namespace.

  If _USE_GOOGLE_APPS_DOMAIN, we allow the namespace manager to infer
  the namespace from the request.

  If _USE_COOKIE, then the ISV might have a gateway page that sets a
  cookie called 'namespace', and we set the namespace to the cookie's value
  """
  name = None

  if _NAMESPACE_PICKER == _USE_SERVER_NAME:
    name = os.environ['SERVER_NAME']
  elif _NAMESPACE_PICKER == _USE_GOOGLE_APPS_DOMAIN:
    name = namespace_manager.google_apps_namespace()
  elif _NAMESPACE_PICKER == _USE_COOKIE:
    cookies = os.environ.get('HTTP_COOKIE', None)
    if cookies:
      name = Cookie.BaseCookie(cookies).get('namespace')

  return name

I suppose I should use the namespace manager, get the namespace and set the analytics ID according to the namespace but how?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演出会有结束 2024-12-14 02:21:07

最简单的方法是使用 Python 字典:

analytics_ids = {
  'mydomain.br': 'UA-637933-12',
  'mydomain.com': 'UA-637933-18',
}

data['analytics'] = analytics_ids[self.request.host]

如果您有其他每个域的统计信息,您可能希望将每个字典条目设为元组、嵌套字典或某种配置对象,然后获取并存储它目前的要求,方便参考。

如果您希望能够在运行时重新配置它,您可以使用数据存储模型,但这会给需要获取它的请求带来额外的延迟;在我看来,每次添加域时重新部署在您的情况下不太可能成为问题。

命名空间与您正在做的事情无关。它们是在不同域之间划分其余数据的好方法,但它们对于划分配置数据没有用处。

The simplest way to do this is with a Python dict:

analytics_ids = {
  'mydomain.br': 'UA-637933-12',
  'mydomain.com': 'UA-637933-18',
}

data['analytics'] = analytics_ids[self.request.host]

If you have other per-domain stats, you may want to make each dictionary entry a tuple, a nested dict, or a configuration object of some sort, then fetch and store it against the current request for easy reference.

If you want to be able to reconfigure this at runtime, you could use a datastore model, but that will impose extra latency on requests that need to fetch it; it seems likely to me that redeploying each time you add a domain isn't likely to be a problem in your case.

Namespaces are tangential to what you're doing. They're a good way to divide up the rest of your data between different domains, but they're not useful for dividing up configuration data.

感情洁癖 2024-12-14 02:21:07

我假设您正在运行同一应用程序的两个实例。
我建议您将 Analytics ID 转换为配置变量,而不是摆弄名称空间。

也就是说,将其存储在配置文件或您的网站正在使用的数据库中。然后为每个部署(在运行 Web 的每个位置)设置一个 ID,并在运行时获取它。

例如:

配置文件:

analyticsId="UA-637933-12"

代码:

data[u'analytics'] = getValueFromConfig("analyticsId")

其中 getValueFromConfig 是您定义的用于读取适当值的函数。 (要轻松使用配置文件,您可以使用 ConfigParser 模块。)

现在您已经获得了更多的灵活性 - 您不必在运行时进行任何检查和切换。您只需为每个网站定义一次该值即可完成。

I presume you have two instances of the same application running.
Instead of fiddling with namespaces, I suggest you turn the Analytics ID into a configuration variable.

That is, either store it in a config file or a database your web is using. Then set one ID for each deployment (in each place your web is running from) and fetch it in the runtime.

For example:

Config file:

analyticsId="UA-637933-12"

Code:

data[u'analytics'] = getValueFromConfig("analyticsId")

where getValueFromConfig is a function you define to read the appropriate value. (To use configuration files effortlessly, you may use the ConfigParser module.)

Now you've gained a lot more flexibility - you don't have to do any checking and switching at runtime. You only have to define the value once per web site and be done with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文