Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for inserting GA4 import fixtures #4004

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ config :plausible, PlausibleWeb.Endpoint,

config :phoenix, :stacktrace_depth, 20
config :phoenix, :plug_init_mode, :runtime

config :plausible, http_impl: Plausible.HTTPClient.Mock
72 changes: 72 additions & 0 deletions lib/plausible/data_migration/generate_local_imported.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
defmodule Plausible.DataMigration.GenerateLocalImported do
import Mox

import Plausible.Factory

alias Plausible.Repo
alias Plausible.Imported.GoogleAnalytics4

@refresh_token_body Jason.decode!(File.read!("fixture/ga_refresh_token.json"))

@full_report_mock [
"fixture/ga4_report_imported_visitors.json",
"fixture/ga4_report_imported_sources.json",
"fixture/ga4_report_imported_pages.json",
"fixture/ga4_report_imported_entry_pages.json",
"fixture/ga4_report_imported_locations.json",
"fixture/ga4_report_imported_devices.json",
"fixture/ga4_report_imported_browsers.json",
"fixture/ga4_report_imported_operating_systems.json"
]
|> Enum.map(&File.read!/1)
|> Enum.map(&Jason.decode!/1)

Mox.defmock(Plausible.HTTPClient.Mock, for: Plausible.HTTPClient.Interface)

def run() do
user = insert(:user, email: "[email protected]")
site = insert(:site, members: [user], domain: "also.unique")
IO.inspect(site.id, label: "Generating imported data per fixtures for site_id")

past = DateTime.add(DateTime.utc_now(), -3600, :second)

{:ok, job} =
Plausible.Imported.GoogleAnalytics4.new_import(
site,
user,
label: "properties/123456",
property: "properties/123456",
start_date: ~D[2024-01-01],
end_date: ~D[2024-01-31],
access_token: "redacted_access_token",
refresh_token: "redacted_refresh_token",
token_expires_at: DateTime.to_iso8601(past)
)

site_import = Plausible.Imported.get_import(site, job.args.import_id)

opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()

opts = Keyword.put(opts, :flush_interval_ms, 10)

expect(Plausible.HTTPClient.Mock, :post, fn "https://www.googleapis.com/oauth2/v4/token",
_headers,
_body ->
{:ok, %Finch.Response{status: 200, body: @refresh_token_body}}
end)

for report <- @full_report_mock do
expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _body, _opts ->
{:ok, %Finch.Response{status: 200, body: report}}
end)
end

:ok = GoogleAnalytics4.import_data(site_import, opts)

site_import
|> Plausible.Imported.SiteImport.complete_changeset()
|> Repo.update!()

IO.inspect(site_import.id, label: "site_import created with import_id")
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Plausible.MixProject do
{:jason, "~> 1.3"},
{:kaffy, "~> 0.10.2", only: [:dev, :test, :staging, :prod]},
{:location, git: "https://github.com/plausible/location.git"},
{:mox, "~> 1.0", only: [:test, :ce_test]},
{:mox, "~> 1.0", only: [:test, :ce_test, :dev]},
{:nanoid, "~> 2.1.0"},
{:nimble_totp, "~> 1.0"},
{:oban, "~> 2.17.0"},
Expand Down