{"id":21237749,"url":"https://github.com/romainvialard/analyticsmp","last_synced_at":"2025-07-10T18:32:24.732Z","repository":{"id":57139471,"uuid":"135267462","full_name":"RomainVialard/AnalyticsMP","owner":"RomainVialard","description":"Make calls to the Google Analytics Measurement Protocol from Google Apps Script","archived":false,"fork":false,"pushed_at":"2020-10-06T07:34:02.000Z","size":11,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T02:18:03.800Z","etag":null,"topics":["apps-script","google-analytics","google-apps-script"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RomainVialard.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-29T08:45:42.000Z","updated_at":"2024-10-08T12:35:07.000Z","dependencies_parsed_at":"2022-09-05T01:11:13.634Z","dependency_job_id":null,"html_url":"https://github.com/RomainVialard/AnalyticsMP","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/RomainVialard/AnalyticsMP","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RomainVialard%2FAnalyticsMP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RomainVialard%2FAnalyticsMP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RomainVialard%2FAnalyticsMP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RomainVialard%2FAnalyticsMP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RomainVialard","download_url":"https://codeload.github.com/RomainVialard/AnalyticsMP/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RomainVialard%2FAnalyticsMP/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264631213,"owners_count":23640941,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apps-script","google-analytics","google-apps-script"],"created_at":"2024-11-21T00:20:32.618Z","updated_at":"2025-07-10T18:32:24.484Z","avatar_url":"https://github.com/RomainVialard.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AnalyticsMP\n\nThis is a library for **Google Apps Script** projects. It provides methods to send data to Google Analytics from server side via the [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/) (hence the name 'AnalyticsMP').\n\nOn client side, a [client ID](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#clientId) is stored in the browsers cookies, \"_so subsequent visits to the same site can be associated with the same user_\". Here a server-side client ID is automatically generated and saved among the [User Properties](https://developers.google.com/apps-script/guides/properties) of your project. If you want to use Google Analytics both on server side and client side (from the HTML Service), it is advised to retrieve the client ID generated on server side (via the method getAnalyticsClientId() of this library) and reuse it on the client side so that all calls are performed with the same client ID for a given user. \n\nFor more information read this blog post: \n\n[Google Apps Script: Tracking add-on usage with Google Analytics](https://cloud.google.com/blog/products/application-development/google-apps-script-tracking-add-on).\n\n\n# Methods\n\n\n## sendAnalyticsEvent(parameters, optPropertyStore)\n\nUse this method to send events (default) or pageviews to Google Analytics. The list of all possible parameters is available here:\n\n[https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters)\n\n\n### Example\n\n```JS\n\n// The tracking ID / web property ID can be set as a global variable named ANALYTICS_TRACKING_ID or can be added in each call via the 'tid' parameter.\n\n  var ANALYTICS_TRACKING_ID = 'UA-17845631-4';\n\n  AnalyticsMP.sendAnalyticsEvent({\n\n    'ec': 'Add-on installed',\n\n    'ea': Session.getActiveUserLocale()\n\n  });\n\n```\n\n\n### Parameters\n\n\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eName\n   \u003c/td\u003e\n   \u003ctd\u003eType\n   \u003c/td\u003e\n   \u003ctd\u003eDescription\n   \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eparameters\n   \u003c/td\u003e\n   \u003ctd\u003eObject\n   \u003c/td\u003e\n   \u003ctd\u003eParameters for the Measurement Protocol, full list documented \u003ca href=\"https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters\"\u003ehere\u003c/a\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eoptPropertyStore\n   \u003c/td\u003e\n   \u003ctd\u003e\u003ca href=\"https://developers.google.com/apps-script/reference/properties/properties\"\u003eProperties\u003c/a\u003e\n   \u003c/td\u003e\n   \u003ctd\u003eOptional - useful to avoid making too many calls to PropertiesService.getUserProperties() or simply to switch to another property store (Document / Script)\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n\n## getAnalyticsClientId(optPropertyStore)\n\nGenerate a unique user ID if none was previously stored in a PropertyService or return the existing stored UUID.\n\nOn server side, the client ID should always be the same for a given user. On client side it is usually stored as a cookie (and thus is usually browser / device related instead of user related).\n\nHere it makes sense to save it as a User Property. This method can also be called from client side to use the same client ID on both server \u0026 client (useful for [session aggregation](https://support.google.com/analytics/answer/2731565?hl=en)).\n\n\n### Parameters\n\n\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eName\n   \u003c/td\u003e\n   \u003ctd\u003eType\n   \u003c/td\u003e\n   \u003ctd\u003eDescription\n   \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eoptPropertyStore\n   \u003c/td\u003e\n   \u003ctd\u003e\u003ca href=\"https://developers.google.com/apps-script/reference/properties/properties\"\u003eProperties\u003c/a\u003e\n   \u003c/td\u003e\n   \u003ctd\u003eOptional - useful to avoid making too many calls to PropertiesService.getUserProperties() or simply to switch to another property store (Document / Script)\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n\n## generateAnalyticsTrackingUrl(parameters, optPropertyStore)\n\nBuild a tracking url, useful eg: to place a tracking beacon in emails sent:\n\nhttps://developers.google.com/analytics/devguides/collection/protocol/v1/email\n\n\n### Parameters\n\n\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eName\n   \u003c/td\u003e\n   \u003ctd\u003eType\n   \u003c/td\u003e\n   \u003ctd\u003eDescription\n   \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eparameters\n   \u003c/td\u003e\n   \u003ctd\u003eObject\n   \u003c/td\u003e\n   \u003ctd\u003eParameters for the Measurement Protocol, full list documented \u003ca href=\"https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters\"\u003ehere\u003c/a\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003eoptPropertyStore\n   \u003c/td\u003e\n   \u003ctd\u003e\u003ca href=\"https://developers.google.com/apps-script/reference/properties/properties\"\u003eProperties\u003c/a\u003e\n   \u003c/td\u003e\n   \u003ctd\u003eOptional - useful to avoid making too many calls to PropertiesService.getUserProperties() or simply to switch to another property store (Document / Script)\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n\n# Setup\n\nYou can copy the code of this library in your own Google Apps Script project or reuse it as a [standard library](https://developers.google.com/apps-script/guides/libraries). In both cases, methods are called using the AnalyticsMP class / namespace, meaning you will use them exactly in the same way.\n\nTo install it as a library, use the following script ID and select the latest version:\n\n`1Bw6UvY6EUalhtNbuwF6TyemIUHCxPZg-HoSHlUfbVbwYqvY9ZKu0mNMO`\n\nTo copy the code in your project, simply copy-past the content of this file in a new script file in your project:\n\n[https://github.com/RomainVialard/AnalyticsMP/blob/master/src/AnalyticsMP.js](https://github.com/RomainVialard/AnalyticsMP/blob/master/src/AnalyticsMP.js)\n\nIt is recommended - but not mandatory - to also include in your project the [ErrorHandler](https://github.com/RomainVialard/ErrorHandler) library. If installed, it will automatically be used by AnalyticsMP methods to perform an Exponential backoff logic whenever it is needed.\n\n\n# Warning\n\nThis library contains 3 methods directly available as functions and callable without using the AnalyticsMP class / namespace:\n\n\n\n*   sendAnalyticsEvent()\n*   getAnalyticsClientId()\n*   generateAnalyticsTrackingUrl()\n\nFor this reason, if you copy the code in your project, make sure you don't have any other function with the exact same name.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromainvialard%2Fanalyticsmp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fromainvialard%2Fanalyticsmp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromainvialard%2Fanalyticsmp/lists"}